调整PNG/JPEG图像大小

3
<?php

header('Content-type:image/gif');
header('Content-Disposition: attachment; filename="animated.gif"');

require_once('GIFEncoder.class.php');

$image = imagecreatefrompng('source01.png');

$width = 50;
$height = 50;
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, 200, 115); 

$text_color = imagecolorallocate($image, 50, 50, 50);
imagestring($image, 5, 5, 5,  '', $text_color);

/* $imageMagick = new Imagick($image);
$imageMagick->adaptiveResizeImage(50,50); */

ob_start();
imagegif($image);
$frames[]=ob_get_contents();
$framed[]=40; 
ob_end_clean();


$gif = new GIFEncoder($frames,$framed,0,2,0,0,0,'bin');
echo $gif->GetAnimation();

//$fp = fopen('animegif.gif', 'w');
//fwrite($fp, $gif->GetAnimation());
//fclose($fp);

?>

有没有办法通过代码将.png图像调整为静态的50x50px大小?谢谢。

2个回答

4

我建议使用imagecopyresampled函数

使用方法如下:

$width = 50;
$height = 50;
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $image.getWidth)=(), $image.getHeight());

已经更新了您的示例,但是生成的 gif(基于 .png)仍然保持其原始大小。我错过了什么吗? :-) - Johan
你没有使用 $new_image。 - Mads

0

我尝试了最后一个链接。当我使用它时(请参见已注释的行),我得到了一个无法读取的gif文件:/ - Johan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接