note 104556 modified in function.imagecopyresized by danbrown
| From: | danbrown@php.net | Date: | Thu, 23 Jun 2011 14:32:21 +0000 |
| Subject: | note 104556 modified in function.imagecopyresized by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180880@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.
* ************************* */
$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.
--was--
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.
http://php.net/manual/en/function.imagecopyresized.php