在file_get_contents之后调整php中的图像大小


Resizing image in php after file_get_contents

谁能告诉我如何解决以下问题?

include('php/resizeImage.php');
if($_POST['uploadlink']){
    $url = $_POST['uploadlink'];
    $urlImage = file_get_contents($url);
    if ($_POST['filename']){
        $filename = $_POST['filename'].".jpg";
    } else {
        $urlinfo = parse_url($url);
        $filename = basename($urlinfo['path']);
    }
    $image = new ResizeImage();
    $image->load($filename);
    $image->resizeToWidth(300);
    $image->save($filename);
    file_put_contents("images/upload/".$filename, $urlImage);
  }

在我从URL收到file_get_contents的图像数据后,我想通过我的resizeImage脚本来调整它的大小,该脚本将图像的文件名作为参数。

编辑:ResizeImage函数load和resizeToWidth:

function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }

当用户通过input type='file'选择本地图像时,我没有遇到任何问题。

    if (isset($_FILES["uploadedfile"])){
        $ufilename = $_FILES["uploadedfile"]["name"];
        $ufiletmpname = $_FILES["uploadedfile"]["tmp_name"];
        $image = new ResizeImage();
        $image->load($ufiletmpname);
        $image->resizeToWidth(300);
        $image->save($ufiletmpname);
}

另一个问题:我将用户名转发到我的脚本,因为我想为每个用户创建一个单独的文件夹,这样他们只能看到自己上传的图像。

$admin = $_GET['admin'];
file_put_contents("images/upload/".$admin."/".$filename, $urlImage);

为什么这对我不起作用?

谢谢。

简单。
只需更改 ResizeImage类的代码,使其能够操作除了文件名之外的图像二进制内容。

你的第二个问题也很简单。
设置PHP安装,使其在屏幕上显示错误(当然是针对开发服务器!),然后您将看到问题"为什么这对我不起作用?"的确切答案。

error_reporting(E_ALL);
ini_set('display_errors',1);

通常帮助。
(也要确保你的代码不做任何HTTP重定向,这可能会隐藏错误消息)

什么是ResizeImage?

如果是我,我会这样做:

$data = file_get_contents($name);
$image = imagecreatefromstring($data);
// resize the image

对于你的第一个问题,ResizeImage是你自己的类,或者你从网上下载的东西。为了帮助你,我们需要看到它。

对于第二部分,file_put_contents不会为您创建目录,要做到这一点,您需要使用mkdir函数。