Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Square thumbnail php

Status
Not open for further replies.

stevehart808

Programmer
Nov 5, 2008
6
GB
Hi everyone,

Any ideas how to change this code so I can have a square thumbnails instead of always the same width but different heights?

Thanks

Code:
function CreateSiteThumbnail($srcFile, $destFile, $width, $quality = 100)
{
	$thumbnail = '';
	
	if (file_exists($srcFile)  && isset($destFile))
	{
		$size        = getimagesize($srcFile);
		$w           = number_format($width, 0, ',', '');
		$h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
		
		$thumbnail =  CopySiteImage($srcFile, $destFile, $w, $h, $quality);
	}
	
	// return the thumbnail file name on sucess or blank on fail
	return basename($thumbnail);
}

/*
	Copy an image to a destination file. The destination
	image size will be $w X $h pixels
*/
function CopySiteImage($srcFile, $destFile, $w, $h, $quality = 100)
{
    $tmpSrc     = pathinfo(strtolower($srcFile));
    $tmpDest    = pathinfo(strtolower($destFile));
    $size       = getimagesize($srcFile);

    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
    {
       $destFile  = substr_replace($destFile, 'jpg', -3);
       $dest      = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } elseif ($tmpDest['extension'] == "png") {
       $dest = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } else {
      return false;
    }

    switch($size[2])
    {
       case 1:       //GIF
           $src = imagecreatefromgif($srcFile);
           break;
       case 2:       //JPEG
           $src = imagecreatefromjpeg($srcFile);
           break;
       case 3:       //PNG
           $src = imagecreatefrompng($srcFile);
           break;
       default:
           return false;
           break;
    }

    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

    switch($size[2])
    {
       case 1:
       case 2:
           imagejpeg($dest,$destFile, $quality);
           break;
       case 3:
           imagepng($dest,$destFile);
    }
    return $destFile;

}

// ------ //
 
To make the height of the thumbnail to be the same as the width, just set $h to $w.
 
Could you explain, if you set $h to $w
you should get a square img
but how will you keep the relationship between H & W if you first do not crop it?
 
In order to maintain your aspect ratio but still output a square, your going to have an image with empty space. I think this is what you want so I fixed up your a code a bit, try this:

Code:
<?
function CreateSiteThumbnail($srcFile, $destFile, $maxWidth_N_Height, $quality = 100)
{
    $thumbnail = '';
    
    if (file_exists($srcFile)  && isset($destFile))
    {
        $size        = getimagesize($srcFile); // Get image size
        if ($size[0] > $size[1]){ // Width is larger then height
        	$w = number_format($maxWidth_N_Height,0,"","");
        	$h = number_format((($size[1] / $size[0]) * $maxWidth_N_Height), 0, "", "");
        }else if ($size[1] > $size[0]){ // Height is larger then width
        	$h = number_format($maxWidth_N_Height,0,"","");
        	$w = number_format((($size[0] / $size[1]) * $maxWidth_N_Height), 0, "", "");
        }else{ // Already square
        	$h = number_format($maxWidth_N_Height,0,"","");
        	$w = number_format($maxWidth_N_Height,0,"","");
        }
        $thumbnail =  CopySiteImage($srcFile, $destFile, $w, $h, $maxWidth_N_Height, $quality);
    }
    // return the thumbnail file name on success or blank on fail
    return basename($thumbnail);
}

/*
    Copy an image to a destination file.
    The destination image size will be $maxWidth_N_Height X $maxWidth_N_Height pixels
    with the image centered in the middle. (Only way to maintain aspect ratio)
*/
function CopySiteImage($srcFile, $destFile, $w, $h, $maxWidth_N_Height, $quality = 100)
{
    $tmpSrc     = pathinfo(strtolower($srcFile));
    $tmpDest    = pathinfo(strtolower($destFile));
    $size       = getimagesize($srcFile);

    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
    {
       $destFile  = substr_replace($destFile, 'jpg', -3);
       $dest      = imagecreatetruecolor($maxWidth_N_Height, $maxWidth_N_Height);
    } elseif ($tmpDest['extension'] == "png") {
       $dest = imagecreatetruecolor($maxWidth_N_Height, $maxWidth_N_Height);
    } else {
      return false;
    }

    switch($size[2])
    {
       case 1:       //GIF
           $src = imagecreatefromgif($srcFile);break;
       case 2:       //JPEG
           $src = imagecreatefromjpeg($srcFile);break;
       case 3:       //PNG
           $src = imagecreatefrompng($srcFile);break;
       default:
           return false;break;
    }

    if ($size[0] > $size[1]){ 
    	// Width is larger then height
    	// We need to center the height of the img
    	$height_adjust = ($w - $h) / 2;
    	imagecopyresampled($dest, $src, 0, $height_adjust, 0, 0, $w, $h, $size[0], $size[1]);
    }else if ($size[1] > $size[0]){ 
    	// Height is larger then width
    	// We need to center the width of the img
    	$width_adjust = ($h - $w) / 2;
    	imagecopyresampled($dest, $src, $width_adjust, 0, 0, 0, $w, $h, $size[0], $size[1]);
    }else{
    	// Already square
    	imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
    }

    switch($size[2])
    {
       case 1: // FYI: This forces gif files to be turned into jpegs
       case 2:
           imagejpeg($dest,$destFile, $quality);
           break;
       case 3:
           imagepng($dest,$destFile);
    }
    return $destFile;

}

$imgtodisplay = CreateSiteThumbnail("img.jpg","thumb_img.gif",300);
?>
<img src="<?=$imgtodisplay?>">

Let us know how it goes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top