Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有水印和调整大小的php图像上载_Javascript_Php_File Upload_Image Uploading_Image Upload - Fatal编程技术网

Javascript 带有水印和调整大小的php图像上载

Javascript 带有水印和调整大小的php图像上载,javascript,php,file-upload,image-uploading,image-upload,Javascript,Php,File Upload,Image Uploading,Image Upload,我有下面的代码将图片上传到我的网站,但是上传的图片看起来很大,需要时间在网站上加载,我想让它们变小,并且我想从我自己的网站上添加水印 我希望图像能像1080X1080那样显示,但质量很好,我试图缩小有限的上传大小,但有些用户不知道如何调整照片的大小 $total=count($_文件['document']['name']); 如果($total>0){ $total=计数($_文件['document']['name']); 如果($total,则需要使用PHP或函数来处理图像 以GD为例,

我有下面的代码将图片上传到我的网站,但是上传的图片看起来很大,需要时间在网站上加载,我想让它们变小,并且我想从我自己的网站上添加水印

我希望图像能像1080X1080那样显示,但质量很好,我试图缩小有限的上传大小,但有些用户不知道如何调整照片的大小

$total=count($_文件['document']['name']);
如果($total>0){
$total=计数($_文件['document']['name']);

如果($total,则需要使用PHP或函数来处理图像

以GD为例,它简单到

function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}
你可以调用这个函数,就像这样

$img = resize_image(‘/path/to/some/image.jpg’, 200, 200);
从个人经验来看,GD的图像重采样也大大减少了文件大小,尤其是在对原始数码相机图像进行重采样时

和水印

PHP手册中的A:

// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

您需要使用PHP或函数来处理图像

以GD为例,它简单到

function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}
你可以调用这个函数,就像这样

$img = resize_image(‘/path/to/some/image.jpg’, 200, 200);
从个人经验来看,GD的图像重采样也大大减少了文件大小,尤其是在对原始数码相机图像进行重采样时

和水印

PHP手册中的A:

// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

我能把它实现到我的代码中吗?我试过了,它给了我错误!!@Ronak Dhootar你使用GD或Imagick方法了吗?你得到了什么错误?我能把它实现到我的代码中吗?我试过了,它给了我错误!!@Ronak Dhootar你使用GD或Imagick方法了吗?这回答了你的问题吗?这回答了你吗你的问题?