【问题标题】:php imagecopyresampled adds black backgroundphp imagecopyresampled 添加黑色背景
【发布时间】:2013-07-14 10:06:42
【问题描述】:

我有一个调整图像大小的脚本,它采用 130x81 图像并将其添加到 130x130 图像中,当 imagecopyresampled 函数运行时,它会在剩余的空间中添加黑色背景,即使基本图像是白色的。下面的代码,我真的很感激一些帮助。

我试图合并到 php 创建的 130x130 文件中的图像是:

$width = 130;
$height = 130;
$filename = 'process-add.jpg'; //130x81px jpg
$this->_image = imagecreatefromjpeg($filename);

$background = imagecreatetruecolor(130,130);//create the background 130x130
$whiteBackground = imagecolorallocate($background, 255, 255, 255); 
imagefill($background,0,0,$whiteBackground); // fill the background with white

imagecopyresampled($background, $this->_image,(130-$width)/2,(130-$height)/2, 0, 0, $width, $height, $width, $height); // copy the image to the background

ImageJpeg ($background,null,100); //display

我已经阅读了多个要添加的帖子:

imagealphablending($background, false);

进入应该修复它的代码,但它没有任何区别。

提前致谢!

【问题讨论】:

    标签: php resize gd


    【解决方案1】:

    这个问题已经解决了。问题在于 imagecopyresampled 调用的宽度和高度。请看下面的代码块:

    <?
    
    ini_set('allow_url_fopen', true);
    $filename = 'http://img.yessy.com/1402152287-17201a.jpg'; // 130x81
    $image = imagecreatefromjpeg($filename);
    list($originalWidth, $originalHeight) = getimagesize($filename);
    
    // Size of image to create
    $width = 130;
    $height = 130;
    
    $background = imagecreatetruecolor($width, $height);//create the background 130x130
    $whiteBackground = imagecolorallocate($background, 255, 255, 255); 
    imagefill($background,0,0,$whiteBackground); // fill the background with white
    
    imagecopyresampled($background, $image, 0, ($height - $originalHeight) / 2, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight); // copy the image to the background
    
    header("Content-type: image/jpeg");
    ImageJpeg ($background,null,100); //display
    ?>
    

    【讨论】:

    • 有人能解释一下为什么会这样吗?我有类似的情况,上传被任意放置在已知高度/宽度的“边界框”中,并且很想知道如何找出我需要的正确值。
    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2016-01-30
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多