note 79119 added to function.imagecopyresampled

From: Date: Mon, 12 Nov 2007 13:32:04 +0000
Subject: note 79119 added to function.imagecopyresampled
Groups: php.notes 
Request: Send a blank email to php-notes+get-132202@lists.php.net to get a copy of this message
This function is taken from lots of web sources so thanks to all for posting. It creates square or landscape thumbnails from .jpgs from either portait or landscape original images. I decide in advance which way I want the thumbs to display for consistency. I usually sharpen the images as well post-upload, to save on server resources. I can post this code if anyone wants it. I hope this helps someone... # create thumbnails from jpgs # usage: # create_jpgthumb(uploaded file, final file (with path), thumb height, thumb width, jpg quality, scale thumb (true) or fixed size (false); function create_jpgthumb($original, $thumbnail, $max_width, $max_height, $quality, $scale = true) { list ($src_width, $src_height, $type, $w) = getimagesize($original); if (!$srcImage = @imagecreatefromjpeg($original)) { return false; } # image resizes to natural height and width if ($scale == true) { if ($src_width > $src_height ) { $thumb_width = $max_width; $thumb_height = floor($src_height * ($max_width / $src_width)); } else if ($src_width < $src_height ) { $thumb_height = $max_height; $thumb_width = floor($src_width * ($max_height / $src_height)); } else { $thumb_width = $max_height; $thumb_height = $max_height; } if (!@$destImage = imagecreatetruecolor($thumb_width, $thumb_height)) { return false; } if (!@imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $thumb_width, $thumb_height, $src_width, $src_height)) { return false; } # image is fixed to supplied width and height and cropped } else if ($scale == false) { $ratio = $max_width / $max_height; # thumbnail is landscape if ($ratio > 1) { # uploaded pic is landscape if ($src_width > $src_height) { $thumb_width = $max_width; $thumb_height = ceil($max_width * ($src_height / $src_width)); if ($thumb_height > $max_width) { $thumb_height = $max_width; $thumb_width = ceil($max_width * ($src_width / $src_height)); } # uploaded pic is portrait } else { $thumb_height = $max_width; $thumb_width = ceil($max_width * ($src_height / $src_width)); if ($thumb_width > $max_width) { $thumb_width = $max_width; $thumb_height = ceil($max_width * ($src_height / $src_width)); } $off_h = ($src_height - $src_width) / 2; } if (!@$destImage = imagecreatetruecolor($max_width, $max_height)) { return false; } if (!@imagecopyresampled($destImage, $srcImage, 0, 0, 0, $off_h, $thumb_width, $thumb_height, $src_width, $src_height)) { return false; } # thumbnail is square } else { if ($src_width > $src_height) { $off_w = ($src_width - $src_height) / 2; $off_h = 0; $src_width = $src_height; } else if ($src_height > $src_width) { $off_w = 0; $off_h = ($src_height - $src_width) / 2; $src_height = $src_width; } else { $off_w = 0; $off_h = 0; } if (!@$destImage = imagecreatetruecolor($max_width, $max_height)) { return false; } if (!@imagecopyresampled($destImage, $srcImage, 0, 0, $off_w, $off_h, $max_width, $max_height, $src_width, $src_height)) { return false; } } } @imagedestroy($srcImage); if (!@imageantialias($destImage, true)) { return false; } if (!@imagejpeg($destImage, $thumbnail, $quality)) { return false; } @imagedestroy($destImage); return true; } ---- Server IP: 87.124.64.96 Probable Submitter: 80.177.173.22 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php Edit -- https://master.php.net/note/edit/79119 Del: integrated -- https://master.php.net/note/delete/79119/integrated Del: useless -- https://master.php.net/note/delete/79119/useless Del: bad code -- https://master.php.net/note/delete/79119/bad+code Del: spam -- https://master.php.net/note/delete/79119/spam Del: non-english -- https://master.php.net/note/delete/79119/non-english Del: in docs -- https://master.php.net/note/delete/79119/in+docs Del: other reasons-- https://master.php.net/note/delete/79119 Reject -- https://master.php.net/note/reject/79119 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132202) next »