Php 如何通过脚本修改png图像?
我有一个透明的png图像,我想做一个副本,然后裁剪成1x1透明图像 我被困在“裁剪到1x1透明图像”部分 我可以修改现有图像,也可以创建新图像并覆盖现有图像。我相信这两种选择都会奏效。我只是不知道该怎么做,最终得到的是1x1像素的透明png图像 非常感谢您的帮助Php 如何通过脚本修改png图像?,php,image-manipulation,Php,Image Manipulation,我有一个透明的png图像,我想做一个副本,然后裁剪成1x1透明图像 我被困在“裁剪到1x1透明图像”部分 我可以修改现有图像,也可以创建新图像并覆盖现有图像。我相信这两种选择都会奏效。我只是不知道该怎么做,最终得到的是1x1像素的透明png图像 非常感谢您的帮助 function convertImage(){ $file1 = "../myfolder/image.png"; $file2 = "../myfolder/image-previous.png"; if
function convertImage(){
$file1 = "../myfolder/image.png";
$file2 = "../myfolder/image-previous.png";
if (!file_exists($file2))
{
//make a copy 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
???
}
}
使用PHP为您提供的
imagecopyresized
方法
例如:
$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 ...
imagecopy()
对文件无效。它可以在GD图像句柄上工作,您可以使用imagecreatfrom…()
获得该句柄。如果您想复制文件,只需使用copy()
erm,转换为1x1px透明PNG即可?你认为你会在其中包含多少细节?@Kerin,没有。这就是目标。它有效地从布局中删除了图像的可视部分,但它只是设计为临时“隐藏”设置。为什么不创建1x1透明图像并交换它们呢?调用GD或ImageMagick这样做在CPU周期和IO方面是非常浪费的。@Kerin,我同意。没有GD怎么办?