note 104556 added to function.imagecopyresized
| From: | email at vladislav dot net | Date: | Wed, 22 Jun 2011 22:30:28 +0000 |
| Subject: | note 104556 added to function.imagecopyresized | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180855@lists.php.net to get a copy of this message | ||
I didn't find here before any script giving both GIF and PNG transparent images correct, so I
offer you mine.
Here is the code for 'image.php' script to generate a resized or original sized images of
any type (JPEG, GIF, PNG) with respect to possible transparency of GIF and PNG.
The following script can be used in HTML tag IMG (to display an image marked in database with ID
equal to 1 and resized to max 100px) like this:
<img src="image.php?id=1&size=100" />.
<?php
/* *************************
* Script by A. Vladislav I.
* http://vladislav.net
* ************************* */
$id = $_GET['id'];
$size = $_GET['size'];
if(!empty($id))
{
/* Here you may take the source image path from where you want: current direcory, databse etc.
I used the $_GET['id'] for giving the script an ID of the image in database, where its
real name is stored.
I consider later the $f variable responsible to store file name and $type variable responsible to
store mime type of the image. You may use function GetImageSize() to discover the mime type.
So don't forget to set the */
$f = /*...*/ ;
/* and */
$type = /*...*/ ;
/* ... */
}
$imgFunc = '';
switch($type)
{
case 'image/gif':
$img = ImageCreateFromGIF($f);
$imgFunc = 'ImageGIF';
$transparent_index = ImageColorTransparent($img);
if($transparent_index!=(-1)) $transparent_color = ImageColorsForIndex($img,$transparent_index);
break;
case 'image/jpeg':
$img = ImageCreateFromJPEG($f);
$imgFunc = 'ImageJPEG';
break;
case 'image/png':
$img = ImageCreateFromPNG($f);
ImageAlphaBlending($img,true);
ImageSaveAlpha($img,true);
$imgFunc = 'ImagePNG';
break;
default:
die("ERROR - no image found");
break;
}
header("Content-Type: ".$type);
if(!empty($size))
{
list($w,$h) = GetImageSize($f);
if( $w==0 or $h==0 ) die("ERROR - zero image size");
$percent = $size / (($w>$h)?$w:$h);
if($percent>0 and $percent<1)
{
$nw = intval($w*$percent);
$nh = intval($h*$percent);
$img_resized = ImageCreateTrueColor($nw,$nh);
if($type=='image/png')
{
ImageAlphaBlending($img_resized,false);
ImageSaveAlpha($img_resized,true);
}
if(!empty($transparent_color))
{
$transparent_new =
ImageColorAllocate($img_resized,$transparent_color['red'],$transparent_color['green'],$transparent_color['blue']);
$transparent_new_index = ImageColorTransparent($img_resized,$transparent_new);
ImageFill($img_resized, 0,0, $transparent_new_index);
}
if(ImageCopyResized($img_resized,$img, 0,0,0,0, $nw,$nh, $w,$h ))
{
ImageDestroy($img);
$img = $img_resized;
}
}
}
$imgFunc($img);
ImageDestroy($img);
?>
P.S.
The script can be written better (optimized etc.), but I hope you can do it by yourself.
----
Server IP: 89.251.21.32
Probable Submitter: 91.124.217.223
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php
Edit -- https://master.php.net/note/edit/104556
Del: integrated -- https://master.php.net/note/delete/104556/integrated
Del: useless -- https://master.php.net/note/delete/104556/useless
Del: bad code -- https://master.php.net/note/delete/104556/bad+code
Del: spam -- https://master.php.net/note/delete/104556/spam
Del: non-english -- https://master.php.net/note/delete/104556/non-english
Del: in docs -- https://master.php.net/note/delete/104556/in+docs
Del: other reasons-- https://master.php.net/note/delete/104556
Reject -- https://master.php.net/note/reject/104556
Search -- https://master.php.net/manage/user-notes.php