水印上传图像,无需调整图像宽度高度

时间:2016-12-15 07:33:49

标签: php image-resizing watermark

所以我的下面代码就像上传图片一样,它会将图片大小调整为720x450,然后给它添加水印。但我希望不要修改宽度和高度,并将水印放在任何大小的图像的右下角

如果有人可以帮助我吗?

$image_path = "../images/watermark.png";
function watermark_image($oldimage_name, $new_image_name){
    global $image_path;
    list($owidth,$oheight) = getimagesize($oldimage_name);
    $width = 720; $height = 450;    
    $im = imagecreatetruecolor($width, $height);
    $img_src = imagecreatefromjpeg($oldimage_name);
    imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
    $watermark = imagecreatefrompng($image_path);
    list($w_width, $w_height) = getimagesize($image_path);        
    $pos_x = $width - $w_width; 
    $pos_y = $height - $w_height;
    imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
    imagejpeg($im, $new_image_name, 90);
    imagedestroy($im);
    unlink($oldimage_name);
    return true;
}

感谢您的帮助和时间。

1 个答案:

答案 0 :(得分:1)

您提供手动高度和宽度,只需指定图像的原始高度和宽度

$image_path = "../images/watermark.png";
function watermark_image($oldimage_name, $new_image_name){
    global $image_path;
    list($owidth,$oheight) = getimagesize($oldimage_name);
    $width = $owidth; $height = $oheight;    
    $im = imagecreatetruecolor($width, $height);
    $img_src = imagecreatefromjpeg($oldimage_name);
    imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
    $watermark = imagecreatefrompng($image_path);
    list($w_width, $w_height) = getimagesize($image_path);        
    $pos_x = $width - $w_width; 
    $pos_y = $height - $w_height;
    imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
    imagejpeg($im, $new_image_name, 90);
    imagedestroy($im);
    unlink($oldimage_name);
    return true;
}

尝试这将按预期工作。

了解更多信息,请点击此处http://php.net/manual/en/image.examples-watermark.php

相关问题