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

标签 php image url upload 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 函数加载和 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 重定向)

关于php - 在 file_get_contents 之后在 php 中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8051500/

相关文章:

php - MySql 选择日期为上个月或更早日期的记录

php - Twitter api 1.1 未加载所有关注者

php - onbeforeunload 似乎并不总是被触发

swift - 缩放图像 OSX Swift

javascript - 在 JavaScript 中使用 PHP 的 DateTime

php - 通过 php 将照片上传到 linux 服务器 - 一个文件夹可以拥有的最大文件数是多少?

javascript - 当 onclick 事件被激活时禁用 onmouseout 功能

javascript - 使用jquery获取URL内容

php - 尝试截断 URI/URL 的文件部分

javascript - 在 JQuery 中的 URL 内容中添加动态变量