我怎样才能重新调整png的大小?我的代码可以重新调整jpg或gif的大小。但不是png

时间:2016-02-25 03:37:21

标签: php image png transparency crop

如果我放了一个jpg或其他格式图像,它可以正常工作。但是,当我输入png时,它会生成一个文件,但属性大小为0.我需要制作裁剪png和透明背景。 这是我的代码。

$image = $_FILES['image'];
$image_size=$image['size']; 
$image_name=$image['name']; 
$image_type=$image['type']; 


function resize($width, $height, $ori = false){
$sql="SELECT file_id FROM file_info ORDER BY file_id DESC";
        $result=mysql_query($sql);
        $row = mysql_fetch_array($result);

/* Get original image x y*/
list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
/* calculate new image size with ratio */
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
$restrainedQuality = 100;

$date=date('Ymd');
$upload_dir_rel = 'uploads/'.$date;
        $upload_dir_abs = $upload_dir_rel;


         if(!is_dir($upload_dir_abs))
        {
          mkdir($upload_dir_abs,0777,true);
        }


/* new file name */
if($ori == true)
{
    $path = $upload_dir_abs.'/'.'ori'.'_'.$row['file_id'].$_FILES['image']['name']; // file name for image name
}
else {
    $path = $upload_dir_abs.'/'.$width.'x'.$height.'_'.$row['file_id'].$_FILES['image']['name']; // file name for image name
}
/* read binary data from image file */
$imgString = file_get_contents($_FILES['image']['tmp_name']);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h);
/* Save image */
switch ($_FILES['image']['type']) {
case 'image/jpeg':
imagejpeg($tmp, $path, 100);
break;
case 'image/png':
imagepng($tmp, $path, 100);
break;
case 'image/gif':
imagegif($tmp, $path);
break;
default:
exit;
break;
}
return $path;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}

// Banner 525 => 100, Logo 300 => 300, Favicon 32 => 32, TC/Testimonial 1050 => 1050, Signature 400 => 200, 260 => 96, 630 => 240
list($wi, $hi) = getimagesize($_FILES['image']['tmp_name']);
$sizes = array(525 => 100, 300 => 300, 32 => 32, 1050 => 1050, 400 => 200, 260 => 96, 630 => 240 );
foreach ($sizes as $w => $h) {
    $files[] = resize($w, $h);
}
$files[] = resize($wi, $hi, true);
// for database
//$file_name=md5 ($_FILES['image']['name']).$_FILES['image']['name']; // file name for database
$date=date('Ymd');
$file_path='uploads/'.$date;
//mysql_query("insert into image(p_id,path,file_name) values('$id','$file_path','$file_name')");
//echo $file_name; echo $file_path; exit;
$table_name="file_info";
$image_names=$id.$image_name;
mysql_query ("INSERT INTO $table_name (`file_id`, `p_id_home`,`file_name`,`file_path`) 
VALUE (NULL, '$cat_code', '$image_names', '$file_path') ") or die(mysql_error());

?>

这是按阵列裁剪的5个尺寸。

1 个答案:

答案 0 :(得分:0)

以下是使用imagemagick并适用于任何格式的示例。

$myImage = new Imagick();
$myImage->readImage('MapMaster-001.png');
$imgCropsAcross = floor($myImage->getImageWidth()/$cropWidth) - 1;
$imgCropsDown = floor($myImage->getImageHeight()/$cropHeight) - 1;

echo "\n";
echo "The source image MapsMaster-001.png (" . $myImage->getImageWidth() . " x " . $myImage->getImageHeight() . ") will be cropped into " .
    $imgCropsDown * $imgCropsAcross . " ($imgCropsAcross x $imgCropsDown ) titles.\n";
echo "    NOTE: All diminisions are WxH and unit of measure is pixels(px)." . "\n";

for($colCnt = 0; $colCnt <= $imgCropsAcross; $colCnt++)
{
    //echo "Col: $colCnt\n";
    for($rowCnt = 0; $rowCnt <= $imgCropsDown; $rowCnt++)
    {
        //echo "Row: $colCnt ($cropWidth x $cropHeight) x: " . $cropHeight * $colCnt . " y: " . $cropWidth * $rowCnt . "\n";
        $myTitle = clone $myImage;
        $myTitle->cropImage($cropWidth, $cropHeight, $cropHeight * $colCnt, $cropWidth * $rowCnt);
        $fileName = "MapsMaster-001-$colCnt-$rowCnt.jpg";
        echo "Writing $fileName to file system.\n";
        $myTitle->writeImage($fileName);
        $myTitle = null;
    }
}

要更改输出格式,只需更改文件名后缀即可。例如从.jpg到.png

您可以使用phpinfo()查看是否安装了imagemagick。