php 调整大小脚本不适用于透明 gif
我怎样才能让它适用于透明 gif 和 png ?
function resizeImage($image,$newImage,$target_width,$target_height, $type="") {
if (is_file($image)) {
if($type == ".gif"){
$image_org=@imagecreatefromgif($image);
}else{
$image_org=@imagecreatefromjpeg($image);
}
if ($image_org) {
list($w,$h,$type,$attr) = getimagesize($image);
$factor=C_Image_Custom::calcRescaleFactor($w,$h,$target_width,$target_height);
if ($factor>1) {
$image_w = $w / $factor;
$image_h = $h / $factor;
} else {
$image_w = $w;
$image_h = $h;
}
//Note: PHP with GD2.0 required for imagecreatetruecolor
$img_copy = imagecreatetruecolor($image_w, $image_h);
imagecopyresampled($img_copy, $image_org, 0, 0, 0, 0, $image_w, $image_h, $w, $h);
if (@imagejpeg($img_copy, $newImage, 80)) {
chmod($newImage,0777);
} else {
echo("<b>Error: </b>Unable to create image $newImage. Check directory permissions.");
}
imagedestroy($image_org);
imagedestroy($img_copy);
}
}
How Can I get this working for transparent gif's and png's?
function resizeImage($image,$newImage,$target_width,$target_height, $type="") {
if (is_file($image)) {
if($type == ".gif"){
$image_org=@imagecreatefromgif($image);
}else{
$image_org=@imagecreatefromjpeg($image);
}
if ($image_org) {
list($w,$h,$type,$attr) = getimagesize($image);
$factor=C_Image_Custom::calcRescaleFactor($w,$h,$target_width,$target_height);
if ($factor>1) {
$image_w = $w / $factor;
$image_h = $h / $factor;
} else {
$image_w = $w;
$image_h = $h;
}
//Note: PHP with GD2.0 required for imagecreatetruecolor
$img_copy = imagecreatetruecolor($image_w, $image_h);
imagecopyresampled($img_copy, $image_org, 0, 0, 0, 0, $image_w, $image_h, $w, $h);
if (@imagejpeg($img_copy, $newImage, 80)) {
chmod($newImage,0777);
} else {
echo("<b>Error: </b>Unable to create image $newImage. Check directory permissions.");
}
imagedestroy($image_org);
imagedestroy($img_copy);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个函数非常适合我调整 jpg 和透明(或非)gif 的大小:
原始函数来自 PhpToys 1.0,使用透明 .gif 的部分来自 此 PHP 文档注释。
This function works perfectly for me for resizing jpg's and transparent (or not) gif's:
The original function is from PhpToys 1.0, and the part that works with transparent .gifs comes from this PHP docs comment.
我遇到了同样的问题,透明度不适用于 PNG,当它与 PNG 配合使用时,它就不适用于 GIF,我一整天都在寻找解决方案,直到我找到这个函数“Maxim's,智能调整图像大小"
某些变量上只有一些小错误,所以你必须按如下所示分配它们以消除 $trnprt_indx 和 $trnprt_color 的错误
希望这会对您有所帮助
i had the same problem, transparency just don't work for PNG and when it work with PNG it just don't with GIF, I've been looking all day for a solution till i found this function "Maxim's, smart resize image"
There is just small errors on some variables, so you have to assign them as below to remove errors for $trnprt_indx and $trnprt_color
Hope this will help you
看起来您只输出到 jpeg - 它没有透明度。 如果要输出透明度,则需要输出gif或png。
如果你想用颜色替换透明度,我想你需要 php 函数 imagecolorallocatealpha
It looks like you're only outputting to jpeg - which doesn't have transparency. If you want to output the transparency, you need to output a gif or png.
If you want to replace the transparency with a colour, I think you want the php function imagecolorallocatealpha
为什么只有jpeg?
这也适用于 gif:
Why only a jpeg?
This is also for gif:
这确实很旧,但以防万一其他人像我一样难以理解,这里是对我有用的解释:
This is really old but in case some else is struggling to understand as much as I did here's what worked for me with explanation: