note 30716 added to function.imagecopyresized

From: Date: Wed, 26 Mar 2003 16:36:12 +0000
Subject: note 30716 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-45848@lists.php.net to get a copy of this message
In addition to the problem where some users encounter black images as results of ImageCopyResampleBicubic , I suggest you initialize your destination image with imagecreatetruecolor - since we're wrapping around with jpg files anyways :) I use the following function to create thumbnails of any size -larger or smaller than the original image. Just for info - the machine uses a Redhat Linux, Apache/1.3.27 (Unix), PHP/4.3.1 with the GD 2.0. <?php function createthumb($IMAGE_SOURCE,$THUMB_X,$THUMB_Y,$OUTPUT_FILE){ $BACKUP_FILE = $OUTPUT_FILE . "_backup.jpg"; copy($IMAGE_SOURCE,$BACKUP_FILE); $IMAGE_PROPERTIES = GetImageSize($BACKUP_FILE); if (!$IMAGE_PROPERTIES[2] == 2) { return(0); } else { $SRC_IMAGE = ImageCreateFromJPEG($BACKUP_FILE); $SRC_X = ImageSX($SRC_IMAGE); $SRC_Y = ImageSY($SRC_IMAGE); if (($THUMB_Y == "0") AND ($THUMB_X == "0")) { return(0); } elseif ($THUMB_Y == "0") { $SCALEX = $THUMB_X/($SRC_X-1); $THUMB_Y = $SRC_Y*$SCALEX; } elseif ($THUMB_X == "0") { $SCALEY = $THUMB_Y/($SRC_Y-1); $THUMB_X = $SRC_X*$SCALEY; } $THUMB_X = (int)($THUMB_X); $THUMB_Y = (int)($THUMB_Y); $DEST_IMAGE = imagecreatetruecolor($THUMB_X,$THUMB_Y); unlink($BACKUP_FILE); if (!ImageCopyResampleBicubic($DEST_IMAGE, $SRC_IMAGE, 0, 0, 0, 0, $THUMB_X, $THUMB_Y, $SRC_X, $SRC_Y)) { imagedestroy($SRC_IMAGE); imagedestroy($DEST_IMAGE); return(0); } else { imagedestroy($SRC_IMAGE); if (ImageJPEG($DEST_IMAGE,$OUTPUT_FILE)) { imagedestroy($DEST_IMAGE); return(1); } imagedestroy($DEST_IMAGE); } return(0); } } # end createthumb ?> -- http://www.php.net/manual/en/function.imagecopyresized.php http://master.php.net/manage/user-notes.php?action=edit+30716 http://master.php.net/manage/user-notes.php?action=delete+30716 http://master.php.net/manage/user-notes.php?action=reject+30716

« previous php.notes (#45848) next »