PHP GD图像损坏在imagecopy上()

时间:2011-04-02 04:11:11

标签: php image gd corrupt

我在使用PHP 5.3.3时遇到了一些奇怪的问题,我正在尝试为图像添加水印。

$body = @imagecreatefromstring($image_data['body']);
imagejpeg($body, null, 85);

返回:http://i.stack.imgur.com/KJjDi.jpg

$body   = @imagecreatefromstring($image_data['body']);
$logo   = @imagecreatefrompng(APP_ROOT . self::WATERMARK_PATH);

$body_width     = (int) @imagesx($body);
$body_height    = (int) @imagesy($body);

$logo_width     = (int) @imagesx($logo);
$logo_height    = (int) @imagesy($logo);

$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);

imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);

imagejpeg($image, null, 85);

返回:http://i.stack.imgur.com/nwtqZ.jpg

buuuuuuuuut ......如果我将身体(猫)添加到图像中......

$body   = @imagecreatefromstring($image_data['body']);
$logo   = @imagecreatefrompng(APP_ROOT . self::WATERMARK_PATH);

$body_width     = (int) @imagesx($body);
$body_height    = (int) @imagesy($body);

$logo_width     = (int) @imagesx($logo);
$logo_height    = (int) @imagesy($logo);

$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);

imagecopy(
    $image, $body, 
    1, 1, 
    0, 0, $body_width, $body_height
);

imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);

imagejpeg($image, null, 85);

结果为:http://i.stack.imgur.com/Xeb73.jpg

正如你在最后一篇中所看到的,图像的底部是腐败的还是......发生了什么?

1 个答案:

答案 0 :(得分:0)

尝试这段代码我绝对没有错误:

<?php
$body   = imagecreatefromjpeg('http://i.stack.imgur.com/KJjDi.jpg');
$logo   = imagecreatefromjpeg('http://b.vimeocdn.com/ps/161/161028_300.jpg');
$body_width     = (int) imagesx($body);
$body_height    = (int) imagesy($body);
$logo_width     = (int) imagesx($logo);
$logo_height    = (int) imagesy($logo);
$image = imagecreatetruecolor($body_width + 4, $body_height + $logo_height);
imagecopy(
    $image, $body, 
    1, 1, 
    0, 0, $body_width, $body_height
);
imagecopy(
    $image, $logo, 
    intval($body_width / 2) - ceil($logo_width / 2), $body_height, 
    0, 0, $logo_width, $logo_height
);
header('Content-Type: image/jpeg');
imagejpeg($image, null, 85);
?>

我要说它与实际图像有关: - ?