代码之家  ›  专栏  ›  技术社区  ›  johram pong

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

  •  0
  • johram pong  · 技术社区  · 8 年前

    所以我下面的代码是这样工作的:如果我上传图像,它会将图像的大小调整为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 回复  |  直到 8 年前
        1
  •  1
  •   Satish Shinde    8 年前

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

    $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