【问题标题】:watermark upload image without resize image width height水印上传图像而不调整图像宽度高度
【发布时间】:2016-12-15 07:33:48
【问题描述】:

所以我的以下代码就像我上传图片一样,它会将图片大小调整为 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;
}

感谢您的帮助和时间。

【问题讨论】:

    标签: php image-resizing watermark


    【解决方案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

    【讨论】:

    • 欣赏队友,但我需要它在底部的水印它在左上角进行
    • @johrampong 根据您的需要设置 $pos_x 和 $pos_y 值。它会工作
    • 仍然在左上角
    • @johrampong imagecopyresampled($im, $img_src, Expected_x_Pos, expected_y_pos, 0, 0, $width, $height, $owidth, $oheight);像这样更新您的代码 Expected_x_Pos - 将是您所需的 x 值 Expected_y_Pos - 将是您所需的 y 值
    猜你喜欢
    • 2014-04-04
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2013-04-28
    相关资源
    最近更新 更多