【问题标题】:pass Imagick output to another function with Imagick (no saving)使用 Imagick 将 Imagick 输出传递给另一个函数(不保存)
【发布时间】:2018-01-08 02:08:54
【问题描述】:

我在两个单独的类中有两个函数,都有一些 Imagick 处理。 我希望第一个 Imagick 调整图像大小,然后将其发送到另一个 Imagick 实例而不将其保存为文件。

头等舱

         function cropimage($inputfile){
          $image = new Imagick($inputfile);
          $image->cropimage($w, $h, $x, $y);
          # and here I wish to return some $image->output???() which let me to use it in second class
         return ????;
         }

在第二类调整大小

     function resizeImage(){
      $crop = new crop(); // my first class instance
      $image_to_work = $crop->cropimage($this->image); // output from first class
      $image = new Imagick($image_to_work);

      $image->resizeImage($width, $height);
      $image->writeImage($outputfile);
     }

有什么想法吗? 提前致谢。 干杯

【问题讨论】:

    标签: php imagick


    【解决方案1】:

    图像可以作为变量传递:

    function cropimage($inputfile) {
       $image = new Imagick($inputfile);
       $image->cropimage($w, $h, $x, $y);
    
       return $image;
    }
    
    function resizeImageAndSave($image) {
          $image->resizeImage($width, $height);
          $image->writeImage($outputfile);
    }
    
    $image = cropimage("./test.png"); // return the image
    resizeImageAndSave($image); // pass it into the next function
    

    【讨论】:

    • 请更新您的问题,而不是使用 cmets,因为其中的代码不可读。
    【解决方案2】:

    使用会话。

    首先,确保在每个将使用会话的脚本中都有 session_start()

    然后执行$_SESSION['image'] = $image 之类的操作,然后您的下一个脚本可以访问该会话变量。

    【讨论】:

    • 这是一个糟糕的建议,永远不要这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2018-01-10
    • 2014-01-03
    • 2021-12-26
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多