note 52542 deleted from function.imagecopyresampled by cmb
| From: | cmb@php.net | Date: | Sun, 19 May 2019 16:02:38 +0000 |
| Subject: | note 52542 deleted from function.imagecopyresampled by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211067@lists.php.net to get a copy of this message | ||
Note Submitter: del at kartoon dot net
----
This snippet allows you to grab a thumbnail from the center of a large image. This was used for a
client photo gallery for art to give a teaser of the image to come (only a small portion). You
could get dynamic with this value. I also put in a scaling factor in case you want to scale down
first before chopping.
<?php
// The file
$filename = 'moon.jpg';
$percent = 1.0; // if you want to scale down first
$imagethumbsize = 200; // thumbnail size (area cropped in middle of image)
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($imagethumbsize , $imagethumbsize); // true color for best quality
$image = imagecreatefromjpeg($filename);
// basically take this line and put in your versin the -($new_width/2) + ($imagethumbsize/2) &
-($new_height/2) + ($imagethumbsize/2) for
// the 2/3 position in the 3 and 4 place for imagecopyresampled
// -($new_width/2) + ($imagethumbsize/2)
// AND
// -($new_height/2) + ($imagethumbsize/2)
// are the trick
imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) +
($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>