13

im looking to create thumbnails that has 100px by 100px dimension. i've seen many articles explaining the methods but most end up having the width!=height if the dimension ratio is to be kept.

for example, i have a 450px by 350px image. i would like to crop to 100px by 100px. if i were to keep the ratio, i would end up having 100px by 77px. this makes it ugly when im listing these images in a row and column. however, a image without dimension ratio will look terrible as well.

i've seen images from flickr and they look fantastic. for example:
thumbnail: http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
medium size: http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
large size: http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg

tks

Mike
  • 21,301
  • 2
  • 42
  • 65
chrizonline
  • 4,779
  • 17
  • 62
  • 102
  • ohh now i know why someone scolded me for that b4. i must click on the tick sign!!. omg i haven notice it. alright let me see the replies – chrizonline Jul 15 '10 at 14:53

5 Answers5

42

This is done by only using a part of the image as the thumbnail which has a 1:1 aspect ratio (mostly the center of the image). If you look closely you can see it in the flickr thumbnail.

Because you have "crop" in your question, I'm not sure if you didn't already know this, but what do you want to know then?

To use cropping, here is an example:

//Your Image
$imgSrc = "image.jpg";

//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);

//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);

// calculating the part of the image to use for thumbnail
if ($width > $height) {
  $y = 0;
  $x = ($width - $height) / 2;
  $smallestSide = $height;
} else {
  $x = 0;
  $y = ($height - $width) / 2;
  $smallestSide = $width;
}

// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);
Sven Koschnicke
  • 6,523
  • 2
  • 34
  • 49
  • Hi skoschnike *double check if i spelled ur name correctly =)* that has cross my mind. but im clueless on how to do that. care to share?? – chrizonline Jul 15 '10 at 14:57
  • hey sven, i tried ur script and it works great. tk you so much. flawless – chrizonline Jul 16 '10 at 17:16
  • hey sven, i just have a question. using ur script works great. but i realised the quality of image after cropping is not the same as the original. for instance, i took a sample pic from flickr and crop to the same dimension and they look different. any solution to retain the quality?? – chrizonline Jul 23 '10 at 08:52
  • 1
    you can specify the jpg-quality in the imagejpeg() function, see here http://de.php.net/imagejpeg (use NULL for the filename parameter). The default quality is 75. – Sven Koschnicke Jul 23 '10 at 09:20
  • Hi There I like this very much works so well, anyway however that I can use the resulting thumbnail in a page without using the header, as I have other content and it won't work – Justin Erswell Nov 04 '11 at 15:03
  • @JustinErswell Sure, just save the above script as a separate file, e.g. `image.php` and include that file in your page as if it was a normal image file: ``. – Sven Koschnicke May 27 '14 at 06:36
  • 5
    Just a suggestion - a nice little function that we all forget about: $smallestSide = min($height, $width); – Enigma Plus Jun 16 '14 at 09:42
  • Nice little tool to process other image file formats: > $sourceProperties = getimagesize($imgSrc); > > $width = $sourceProperties[0]; > $height = $sourceProperties[1]; > > switch ($sourceProperties[2]) { > case IMAGETYPE_PNG: $myImage = imagecreatefrompng($imgSrc); break; case IMAGETYPE_GIF: $myImage = imagecreatefromgif($imgSrc); break; case IMAGETYPE_JPEG: $myImage = imagecreatefromjpeg($imgSrc); break; – Albuquerque Web Design May 07 '19 at 12:04
3

Crop image with square based on lesser width or height

 public function croppThis($target_url) {

    $this->jpegImgCrop($target_url);

 }

$target_url - is Name of image.

 public function jpegImgCrop($target_url) {//support



  $image = imagecreatefromjpeg($target_url);
  $filename = $target_url;
  $width = imagesx($image);
  $height = imagesy($image);
  $image_type = imagetypes($image); //IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM

  if($width==$height) {

   $thumb_width = $width;
   $thumb_height = $height;

  } elseif($width<$height) {

   $thumb_width = $width;
   $thumb_height = $width;

  } elseif($width>$height) {

   $thumb_width = $height;
   $thumb_height = $height;

  } else {
   $thumb_width = 150;
   $thumb_height = 150;
  }

  $original_aspect = $width / $height;
  $thumb_aspect = $thumb_width / $thumb_height;

  if ( $original_aspect >= $thumb_aspect ) {

     // If image is wider than thumbnail (in aspect ratio sense)
     $new_height = $thumb_height;
     $new_width = $width / ($height / $thumb_height);

  }
  else {
     // If the thumbnail is wider than the image
     $new_width = $thumb_width;
     $new_height = $height / ($width / $thumb_width);
  }

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

  // Resize and crop
  imagecopyresampled($thumb,
         $image,
         0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
         0 - ($new_height - $thumb_height) / 2, // Center the image vertically
         0, 0,
         $new_width, $new_height,
         $width, $height);
  imagejpeg($thumb, $filename, 80);

 }
ShivarajRH
  • 902
  • 12
  • 24
3

You can use this code. You need to pass source image path and thumbnail size in px, and optional destination path. if you pass it will save the image else the thumb will be shown.

Only jpg, jpeg and png are allowed.

function cropImage($sourcePath, $thumbSize, $destination = null) {

  $parts = explode('.', $sourcePath);
  $ext = $parts[count($parts) - 1];
  if ($ext == 'jpg' || $ext == 'jpeg') {
    $format = 'jpg';
  } else {
    $format = 'png';
  }

  if ($format == 'jpg') {
    $sourceImage = imagecreatefromjpeg($sourcePath);
  }
  if ($format == 'png') {
    $sourceImage = imagecreatefrompng($sourcePath);
  }

  list($srcWidth, $srcHeight) = getimagesize($sourcePath);

  // calculating the part of the image to use for thumbnail
  if ($srcWidth > $srcHeight) {
    $y = 0;
    $x = ($srcWidth - $srcHeight) / 2;
    $smallestSide = $srcHeight;
  } else {
    $x = 0;
    $y = ($srcHeight - $srcWidth) / 2;
    $smallestSide = $srcWidth;
  }

  $destinationImage = imagecreatetruecolor($thumbSize, $thumbSize);
  imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

  if ($destination == null) {
    header('Content-Type: image/jpeg');
    if ($format == 'jpg') {
      imagejpeg($destinationImage, null, 100);
    }
    if ($format == 'png') {
      imagejpeg($destinationImage);
    }
    if ($destination = null) {
    }
  } else {
    if ($format == 'jpg') {
      imagejpeg($destinationImage, $destination, 100);
    }
    if ($format == 'png') {
      imagepng($destinationImage, $destination);
    }
  }
}
sajad abbasi
  • 1,988
  • 2
  • 22
  • 43
0

I like using GDLib to fiddle with images, it's fantastically easy to work with also. There are lots of blog posts and tutorials out there. I would recommend using a class for it or similar though, as taking care of all the variations in the image can be very time consuming.

David Yell
  • 11,756
  • 13
  • 61
  • 100
0

To complete @SvenKoschnicke code, here is little tool to process other image formats:

$sourceProperties = getimagesize($imgSrc);

 $width = $sourceProperties[0];

 $height = $sourceProperties[1];

 switch ($sourceProperties[2]) {

 case IMAGETYPE_PNG:
        $myImage = imagecreatefrompng($imgSrc); 
        break;

  case IMAGETYPE_GIF:
        $myImage = imagecreatefromgif($imgSrc); 
        break;

  case IMAGETYPE_JPEG:
        $myImage = imagecreatefromjpeg($imgSrc); 
        break;
 }