How to modify a png image via script?
I have a transparent png image that I would like to make a copy of then crop to a 1x1 transparent image.
I'm stuck on the "crop to a 1x1 transparent image" part.
I can either modify the existing image or create new image and overwrite the existing one. I believe both options would work. I'm just not sure how to do either and end up with a 1x1 pixel transparent png image.
Any help much appreciated.
function convertImage(){
$file1 = "../myfolder/image.png";
$file2 = "../myfolder/image-previous.png";
if (!file_exists($file2))
{
//make a c开发者_如何转开发opy of image.png and name the resulting file image-previous.png
imagecopy($file2, $file1);
// convert image.png to a 1x1 pixel transparent png
// OR
// create a new 1x1 transparent png and overwrite image.png with it
???
}
}
Make use of the imagecopyresized
method that PHP has for you.
More information about imagecopyresized
Example:
$image_stats = GetImageSize("/picture/$photo_filename");
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
$new_w = $cfg_thumb_width;
$ratio = ($imagewidth / $cfg_thumb_width);
$new_h = round($imageheight / $ratio);
// if this is a jpeg, resize as a jpeg
if ($img_type=="2") {
$src_img = imagecreatefromjpeg("/picture/$photo_filename");
$dst_img = imagecreate($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, "/picture/$photo_filename");
}
// if image is a png, copy it as a png
else if ($img_type=="3") {
$dst_img=ImageCreate($new_w,$new_h);
$src_img=ImageCreateFrompng("/picture/$photo_filename");
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
imagepng($dst_img, "/picture/$photo_filename");
}
else ...
精彩评论