【问题标题】:PHP image upload with watermark带水印的PHP图片上传
【发布时间】:2016-03-16 04:54:28
【问题描述】:

我有这段代码可以上传带水印的图片。该代码工作正常,但水印功能将所有上传的图像重新调整为小宽度和高度。添加水印后我想保留大小。我相信问题出在功能上,但我不知道如何解决。

if(isset($_FILES)){
    $file = $_FILES['image'];
    $allowedExts = array('jpg','png','gif','jpeg');
    $uploadsDirectory = "imgupload/";
    $maxSize = 2000000;

    for($i = 0; $i < count($file['name']); $i++){
        $filetmpname = $file['tmp_name'][$i];       
        $errors = array();
        $filename = $file['name'][$i];
        $filetext = strtolower(end(explode('.',$filename)));
        $filesize = $file['size'][$i];
        $filetmpname = $file['tmp_name'][$i];

        if(in_array($filetext, $allowedExts) === FALSE){
            $errors[] = "Extension is not allowed"; 
        }

        if($filesize > $maxSize){
            $errors[] = "File Size must be less than {$maxSize} KB";
        }

        if(empty($errors)){   
            $random = rand(0,199);
            $destination = $file['name'][$i] = $uploadsDirectory. $random."_".date("d-m-Y") . "_" . $file['name'][$i];
            $upload_status = move_uploaded_file($filetmpname, $destination);  

            if($upload_status){
                $new_name = $uploadsDirectory.$random."_".date("d-m-Y") . "_" .".jpg";
                if(watermark_image($destination, $new_name))
                    $demo_image = $new_name;
            }
        }
    }
}

水印功能:

$image_path = "images/water.png"; 

function watermark_image($oldimage_name, $new_image_name)
    {
        global $image_path;
        list($owidth,$oheight) = getimagesize($oldimage_name);
        $width = $height = 300;    
        $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, 100);
        imagedestroy($im);
        unlink($oldimage_name);
        return true;
    } 

【问题讨论】:

    标签: php image upload watermark


    【解决方案1】:

    您在此处获取现有图像的大小:

    list($owidth,$oheight) = getimagesize($oldimage_name);
    $width = $height = 300;    
    

    这是制作不同尺寸图片的地方:

    imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
    

    http://php.net/manual/en/function.imagecopyresampled.php

    【讨论】:

    • 这个水印功能我在其他网站上找到并添加到我的代码中,但图像尺寸重新调整大小我不知道如何解决这个问题请帮助我@ryantxr
    • 查看 PHP 文档。您将看到该函数的完整说明以及它如何调整图像大小。
    • 我现在修复了调整大小的问题,但是如果他给我的图像 png 或 gif(不是有效的 PNG 文件)或(不是有效的 JPG 文件)我有另一个问题如何在` $img_src = imagecreatefromjpeg($oldimage_name);`
    • 不要改变问题。如果答案解决了您的问题,请接受答案并打开一个新问题。
    猜你喜欢
    • 2016-10-07
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2010-11-28
    • 2020-06-13
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    相关资源
    最近更新 更多