Reputation: 3392
I have a php thumbnail function. How it works you can check below:
public static function makeThumb($source, $destination, $thumb_width){
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
$x = 0;
$y = 0;
$status = false;
if ($width > $height) {
$x = ceil(($width - $height) / 2);
$width = $height;
} else if ($height > $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$new_image = imagecreatetruecolor($thumb_width,$thumb_width) or die ('Cannot Initialize new GD image stream');
$extension = self::get_file_extension($source);
if ($extension == 'jpg' || $extension == 'jpeg')
$image = imagecreatefromjpeg($source);
if ($extension == 'gif')
$image = imagecreatefromgif($source);
if ($extension == 'png')
$image = imagecreatefrompng($source);
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
if ($extension == 'jpg' || $extension == 'jpeg')
$status = @imagejpeg($new_image, $destination);
if ($extension == 'gif')
$status = @imagegif($new_image, $destination);
if ($extension == 'png')
$status = @imagepng($new_image, $destination);
imagedestroy($image);
return $status;
}
Please check images below (how it works):
Question: How can I get this image as a result (This thumb is from photoshop)?
Upvotes: 3
Views: 2635
Reputation: 3392
The right solution is here:
public static function makeThumb($source, $destination, $square_size=167, $quality=90) {
$status = false;
list($width, $height, $type, $attr) = getimagesize($source);
if($width> $height) {
$width_t = $square_size;
$height_t = round($height/$width*$square_size);
$off_y = ceil(($width_t-$height_t)/2);
$off_x = 0;
} elseif($height> $width) {
$height_t = $square_size;
$width_t = round($width/$height*$square_size);
$off_x = ceil(($height_t-$width_t)/2);
$off_y = 0;
} else {
$width_t = $height_t = $square_size;
$off_x = $off_y = 0;
}
$thumb_p = imagecreatetruecolor($square_size, $square_size);
$extension = self::get_file_extension($source);
if($extension == "gif" or $extension == "png"){
imagecolortransparent($thumb_p, imagecolorallocatealpha($thumb_p, 0, 0, 0, 127));
imagealphablending($thumb_p, false);
imagesavealpha($thumb_p, true);
}
if ($extension == 'jpg' || $extension == 'jpeg')
$thumb = imagecreatefromjpeg($source);
if ($extension == 'gif')
$thumb = imagecreatefromgif($source);
if ($extension == 'png')
$thumb = imagecreatefrompng($source);
$bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
imagefill ($thumb_p, 0, 0, $bg);
imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
if ($extension == 'jpg' || $extension == 'jpeg')
$status = @imagejpeg($thumb_p,$destination,$quality);
if ($extension == 'gif')
$status = @imagegif($thumb_p,$destination,$quality);
if ($extension == 'png')
$status = @imagepng($thumb_p,$destination,9);
imagedestroy($thumb);
imagedestroy($thumb_p);
return $status;
}
Upvotes: 1
Reputation: 3729
The use of the wrong thumbwidth (and no thumbheight!) is what gives you this square result.
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
According to the PHP manual (http://php.net/manual/en/function.imagecopyresampled.php), imagecopyresampled copies a resized region of the original image. But you want the whole original. You could do
imagecopyresampled($new_image,$image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
You need to calculate $thumbheight first - height*thumbwidth/width would do, or you could divide the original width and height by a set amount.
As the comments below show, the parameters used in this function are rather confusing. So I add an explanation in the hope that you can create what you like.
imagecopyresampled
copies and resizes a rectangular part of an image into a rectangular part of a new one. So all the parameters are needed twice over - for the original and the new image. A drawing might help:
$image $new_image
+----------------+ +----------------+
| source img | | destination img|
| | | |
| | | +--------+ |
| +------+| | | part | |
| | part || | | copy | |
| | || | +--------+ |
| +------+| | |
+----------------+ +----------------+
The arguments are
$dst_image the new image (dst = destination)
$src_image the old image (src = source)
$dst_x , $dst_y top left of destination area
$src_x , $src_y top left of source area
$dst_w , $dst_h width and height of destination area
$src_w , $src_h width and height of source area
If you're trying to copy the whole source image, to a new size, in a new destination image, they will be something like
$dst_image the new image (dst = destination)
$src_image the old image (src = source)
0 , 0 top left of destination area
0 , 0 top left of source area
$dst_w , $dst_h new width and height of thumb
$width , $height width and height of whole source image
But as I'm not certain what size you want the thumbnail, I have to leave you to find the correct values.
Upvotes: 1