2011-07-11 39 views
2

我有一張透明的png圖像,我想製作一張副本然後剪裁成1x1的透明圖像。如何通過腳本修改PNG圖像?

我困在「裁剪爲1x1透明圖像」部分。

我可以修改現有圖像或創建新圖像並覆蓋現有圖像。我相信這兩種選擇都可行。我只是不知道該怎麼做,最終得到1x1像素的透明PNG圖像。

任何幫助非常感謝。

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 
     ??? 

     } 
} 
+0

'imagecopy的()'不上的文件。它適用於GD圖像手柄,你可以使用'imagecreatfrom ...()獲得'如果你想複製一個文件,只需使用'copy()' –

+0

erm,轉換成一個1x1px透明PNG?你有多少細節想象你會打包進去? –

+0

@Kerin,無。這是目標。它有效地從佈局中移除圖像的可視部分,但其唯一設計爲臨時「隱藏」設置。 –

回答

2

利用PHP爲您提供的imagecopyresized方法。

More information about 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 ... 
+0

謝謝朱爾斯。這有幫助! –

+0

不客氣! – Jules