Jesse Elser
Jesse Elser

Reputation: 976

Thumbnail Script Not Creating Square Images

I'm using a thumbnail script that is supposed to create thumbnails for images in my gallery directory. It works great. But one issue. The images are coming out rectangular instead of square. I need the images to come out square. I have the width set to 150 and the height set to 150. I'm not sure what I'm doing wrong. This is a pre-written script I am using that can be found at http://www.foliopages.com/php-photo-gallery-no-database [Not my site, this is the source of the script]

This is the first half of the thumbnail script(there are other functions in between these two parts):

$thumb_width   = '150';      // width of thumbnails
$thumb_height  = '150';       // height of thumbnails
$extensions    = array(".jpg",".png",".gif",".JPG",".PNG",".GIF"); // allowed extensions in photo gallery


// create thumbnails from images
function make_thumb($folder,$src,$dest,$thumb_width) {

    $source_image = imagecreatefromjpeg($folder.'/'.$src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    $thumb_height = floor($height*($thumb_width/$width));

    $virtual_image = imagecreatetruecolor($thumb_width,$thumb_height);

    imagecopyresampled($virtual_image,$source_image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);

    imagejpeg($virtual_image,$dest,100);

}

And the second part:

   $thumb = $src_folder.'/thumbs/'.$file;
           if (!file_exists($thumb)) {
              make_thumb($src_folder,$file,$thumb,$thumb_width); 

           }

Upvotes: 1

Views: 129

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

The issue is that you calculate a value for $thumb_height in the line

$thumb_height = floor($height*($thumb_width/$width));

Replace that line with this one to make the thumb square:

$thumb_height=$thumb_width; 

Upvotes: 1

Related Questions