Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
如何在PHP中调整上载图像的大小_Php_Image - Fatal编程技术网

如何在PHP中调整上载图像的大小

如何在PHP中调整上载图像的大小,php,image,Php,Image,我目前正在使用此代码,以便用户可以将图像上载到我的网站 $allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg','.JPG','.PNG','.BIF','.GIF','.JPEG'); // These will be the types of file that will pass the validation. $max_filesize = 524288; // filesize in BYTES (currentl

我目前正在使用此代码,以便用户可以将图像上载到我的网站

$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg','.JPG','.PNG','.BIF','.GIF','.JPEG'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; //  filesize in BYTES (currently 0.5MB).
$uploadpath = "/home/path/to/file/files/avatars/$_SESSION[user]";
$posted9 = hash('md5',$_SESSION["user"]).$ext; /* not for security mind you. */
// Upload the file to your specified path.
        $f = file_put_contents(
            $upload_path . $posted9,
            file_get_contents('php://input')
            );
        if($f)
    $m="Avatar updated<br/>"; // It worked.
  else
     $m="There was an error during the file upload.  Please try again."; // It failed :(.
$allowed_filetypes=array('.jpg'、'.gif'、'.bmp'、'.png'、'.jpg'、'.jpg'、'.png'、'.BIF'、'.gif'、'.jpeg');//这些将是通过验证的文件类型。
$max_filesize=524288;//以字节为单位的文件大小(当前为0.5MB)。
$uploadpath=“/home/path/to/file/files/avatars/$\u会话[用户]”;
$posted9=hash('md5',$_会话[“用户])。$ext;/*不是为了安全,请注意*/
//将文件上载到指定路径。
$f=文件内容(
$upload_path.$posted9,
文件\u获取\u内容('php://input')
);
如果有的话(f美元)
$m=“头像已更新”;//成功了。
其他的
$m=“上传文件时出错。请重试。”;//它失败了:(。
然而,当这些图像被重新压缩以适应我的网站上的评论时,它们就会失真并失去质量。

如何使用PHP调整图像大小以保持质量

   public function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->image = $new_image;
  } 


结果如下:
比你的好得多
因此,基本上在你上传完文件后,你运行我的代码,你就可以开始了

谷歌上有大量的教程和示例。你甚至懒得去研究它吗?StackOverflow上也有无数类似的问题……RTFM:你问的是图像大小,但只显示上传图像的代码……然后找到一个b比一长串区分大小写的文件扩展名更适合测试文件类型
<?php

// Url of the file
$url = "/homepages/0/d502303335/htdocs/foo.jpg";

// Set a maximum height and width, I used the ones on your picture
$width = 49;
$height = 47;


// Get new dimensions
list($width_orig, $height_orig) = getimagesize($url);

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output, You can use another url, In my case i just overwrite the picture
imagejpeg($image_p, $url);

?>