我正在创建myImage
各种格式的缩略图(jpg, png)
。这是我的代码
<?php
error_reporting(E_ALL);
//$file = 'H:/xampp/htdocs/myfolder/new.jpg';
$file = 'H:/xampp/htdocs/myfolder/phool.png';
$pathToSave = 'H:/xampp/myfolder/New/';
$sourceWidth =60;
$sourceHeight = 60;
$what = getimagesize($file);
$file_name = basename($file);
//print "MIME ".$what['mime'];
switch(strtolower($what['mime']))
{
case 'image/png':
$img = imagecreatefrompng($file);
$img = imagecreatefromstring($img);
$new = imagecreatetruecolor($what[0],$what[1]);
imagecopy($new,$img,0,0,0,0,$what[0],$what[1]);
header('Content-Type: image/png');
imagepng($new,$pathToSave.$file_name);
imagedestroy($new);
break;
case 'image/jpeg':
$img = imagecreatefromjpeg($file);
$new = imagecreatetruecolor($what[0],$what[1]);
imagecopy($new,$img,0,0,0,0,$what[0],$what[1]);
header('Content-Type: image/jpeg');
imagejpeg($new,$pathToSave.$file_name);
imagedestroy($new);
break;
case 'image/gif':
$img = imagecreatefromgif($file);
break;
default: die();
}
但是,它不适用于 PNG 图像。它在我的目的地保存了一个空白图像$pathToSave = 'H:/xampp/htdocs/myfolder/New/'
。
如果我删除$img = imagecreatefromstring($img);
,imagepng
则按原样保存图像,但不会按原样压缩图像imagejpg
。