views:

367

answers:

2

I have a php script im currently using that creates thumbnails based on a max width and height. However, I'd like it to always create square images and crop the images when needed.

Here is what I'm using now:

    function makeThumb( $filename, $type ) {
  global $max_width, $max_height;
  if ( $type == 'jpg' ) {
   $src = imagecreatefromjpeg("blocks/img/gallery/" . $filename);
  } else if ( $type == 'png' ) {
   $src = imagecreatefrompng("blocks/img/gallery/" . $filename);
  } else if ( $type == 'gif' ) {
   $src = imagecreatefromgif("blocks/img/gallery/" . $filename);
  }
  if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
   $newW = $oldW * ($max_width / $oldH);
   $newH = $max_height;
  } else {
   $newW = $max_width;
   $newH = $oldH * ($max_height / $oldW);
  }
  $new = imagecreatetruecolor($newW, $newH);
  imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
  if ( $type == 'jpg' ) {
   imagejpeg($new, 'blocks/img/gallery/thumbs/'.$filename);
  } else if ( $type == 'png' ) {
   imagepng($new, 'blocks/img/gallery/thumbs/'.$filename);
  } else if ( $type == 'gif' ) {
   imagegif($new, 'blocks/img/gallery/thumbs/'.$filename);
  }
  imagedestroy($new);
  imagedestroy($src);
 }

How would I alter this to accomplish what I want (Square thumbs)?

Thanks in advance.

A: 

You want to work out an offset rather than a new width/height so that the new sample stays in proportion, then use the offset when generating the new image and give it a fixed width/height so that it'll crop to a square. A quick example that would make a 100x100 thumb (note: untested),

// Get dimensions of the src image.
list($oldW, $oldH) = getimagesize($filename);

// Work out what offset to use
if ($oldH < $oldW) 
{
    $offH = 0;
    $offW = ($oldW-$oldH)/2;
    $oldW = $oldH;
} 
elseif ($oldH > $oldW) 
{
    $offW = 0;
    $offH = ($oldH-$oldW)/2;
    $oldH = $oldW;
} 
else 
{
    $offW = 0;
    $offH = 0;
}                

// Resample the image into the new dimensions.
$new = imagecreatetruecolor(100, 100);
imagecopyresampled($new, $src, 0, 0, $offW, $offH, 100, 100, $oldW, $oldH);
Rich Adams
Thanks Rich! I'm getting very close. The thumbs are now square, but all black. I think its generating them outside of the image area for some reason.
markf
A: 
function makeThumb( $filename , $thumbSize=100 ){
  global $max_width, $max_height;
 /* Set Filenames */
  $srcFile = 'blocks/img/gallery/'.$filename;
  $thumbFile = 'blocks/img/gallery/thumbs/'.$filename;
 /* Determine the File Type */
  $type = substr( $filename , strrpos( $filename , '.' )+1 );
 /* Create the Source Image */
  switch( $type ){
    case 'jpg' : case 'jpeg' :
      $src = imagecreatefromjpeg( $srcFile ); break;
    case 'png' :
      $src = imagecreatefrompng( $srcFile ); break;
    case 'gif' :
      $src = imagecreatefromgif( $srcFile ); break;
  }
 /* Determine the Image Dimensions */
  $oldW = imagesx( $src );
  $oldH = imagesy( $src );
 /* Calculate the New Image Dimensions */
  if( $oldH > $oldW ){
   /* Portrait */
    $newW = $thumbSize;
    $newH = $oldH * ( $thumbSize / $newW );
  }else{
   /* Landscape */
    $newH = $thumbSize;
    $newW = $oldW * ( $thumbSize / $newH );
  }
 /* Create the New Image */
  $new = imagecreatetruecolor( $thumbSize , $thumbSize );
 /* Transcribe the Source Image into the New (Square) Image */
  imagecopyresampled( $new , $src , 0 , 0 , ( $newW-$thumbSize )/2 , ( $newH-$thumbSize )/2 , $thumbSize , $thumbSize , $oldW , $oldH );
  switch( $type ){
    case 'jpg' : case 'jpeg' :
      $src = imagejpeg( $new , $thumbFile ); break;
    case 'png' :
      $src = imagepng( $new , $thumbFile ); break;
    case 'gif' :
      $src = imagegif( $new , $thumbFile ); break;
  }
  imagedestroy( $new );
  imagedestroy( $src );
}
Lucanos