【问题标题】:Convert image file to webp on upload上传时将图像文件转换为 webp
【发布时间】:2021-08-02 21:16:31
【问题描述】:

除了在不支持 webP 的浏览器中上传原始文件以供后备之外,尝试编写将图像类型(gif、jpeg、jpg、png)转换为 webP 的脚本 (早期版本的 Safari 等)

在尝试让它发挥作用时,我有:

  1. 构建了一个脚本,用于获取所有图像类型并将它们成功上传到服务器,保存三个不同大小的缩略图用于各种用途。
  2. 在 OS X/Mac 上运行的本地 mamp 服务器 (6.4) 上启用 gd 库
  3. 阅读大量关于 webP 的文章
  4. 尝试了几个教程/整理了一堆示例
  5. 尝试研究我的错误 - 在网络上发现很少有用的参考

多次尝试传递数据导致我尝试这样做,以便我可以使用 imagewebp($imgWEBP, $uploadPATH) 将图像转换并上传到服务器从用户的本地主机/桌面到站点服务器映像目录。使用更直接的切换$_FILES['image']['tmp_name'], $_FILES['image']['name'] 转换图像的其他尝试都失败了;目前我正在尝试将图像保存在与脚本相同目录的文件夹中,一旦脚本正常工作,我将在稍后重新路径。

我认为导致错误的重复出现的问题是在图像源/数据的切换中,因此图像数据可以转换为 webP 并上传到服务器。

我发现的教程和示例没有解决如何以可接受的方式传递数据,它们都以数据形式开始,并没有告诉你它是什么形式。

我已经在这个脚本中走得很远,这是我迄今为止尝试过的最高级的脚本。第一次尝试使用 GD 库。我是一个新手,可能有点过头了,但是尝试,我希望我能得到正确的指导来解决错误类型问题(类型必须是 GdImage,给定字符串)

PHP 脚本

#  save image file given from upload form to server in webP format
function   sv___asWEBP ($source, $fileEXTN, $pathSOURCE, $pathUPLOAD, $quality,   $cont= '', $myIMAGE='') {

# var_dump($source);
#die;
#$a_$fileEXTN = pathinfo($source, PATHINFO_EXTENSION);

if($fileEXTN == 'jpeg' || $fileEXTN == 'jpg'){
     
    $myIMAGE = imagecreatefromjpeg($source);
    ob_start();
        imagejpeg($myIMAGE,NULL,100);
        $cont =  ob_get_contents();
    ob_end_clean();
        imagedestroy($myIMAGE);
    }

if($fileEXTN == 'gif'){ 
    $myIMAGE = imagecreatefromgif($source);
    }

if($fileEXTN == 'png'){
    $myIMAGE = imagecreatefrompng($source);
    }

#  convert data to string to attempt convert to webP format
$content =  imagecreatefromstring($cont);

# convert string to webP save to server
$imgWEBP  = imagewebp($cont,  $pathSOURCE); # <=== line #415

# upload / save image to server
imagewebp($imgWEBP, $pathUPLOAD);

#  free resources
imagedestroy($imgWEBP);
}//  sv___asWEBP

总而言之,问题很简单,尝试将图像文件移交给 imagewebP 函数失败。所有尝试都会产生相同的错误“Uncaught TypeError: imagewebp(): Argument #1 ($image) must be of type GdImage, string given”。错误发生在第 #415 行——(参见代码——行有 '#

FULL ERROR MESSAGE
Fatal error: Uncaught TypeError: imagewebp(): Argument #1 ($image) must be of type GdImage, string given in .../a_test/aaaSTORE.php:415 
Stack trace: #0 ...aaaSTORE.php(415): imagewebp('\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x01\x00...', '/Applications/M...') 
#1 .../a_test/aaaSTORE.php(308): sv___asWEBP('/Applications/M...', 'jpg', '/Applications/M...', './a_uploads/bob...', 80) 
#2 ...aaaSTORE.php(121): ex___imgRESIZE(75, 75, 'jpg', '210802205802', './a_uploads/bob...', 'bob_burgerston-...') 
#3 ...aaa.php(1): include('/Applications/M...') 
#4 {main} thrown in ...aaaSTORE.php on line 415

【问题讨论】:

  • imagecreatefromjpeg($source)的返回值,只要不是false,就是你要传给imagewebp()的值
  • 很高兴知道,我不确定这意味着什么,但它为我提供了探索解决方案的新方向。谢谢@ChrisHass

标签: php upload type-conversion gd webp


【解决方案1】:

从此代码中获取参考。在此代码中,您可以看到图像是通过 post 方法接收的,然后简单地保存为 .webp,同时考虑每种图像格式。 这里发生了什么?

图像直接存储为 tmp 文件格式的 webp,因此两者的大小也将相同

//图像通过POST方法打磨

  if(isset($_FILES['image'])){
  $errors= array();

  $file_name = $_FILES['image']['name'];
  $file_size =$_FILES['image']['size'];
  $file_tmp =$_FILES['image']['tmp_name'];
  $file_type=$_FILES['image']['type'];
  $file_ext = explode('.',$file_name)[1]; // taking the extension of uploaded img

  // an option to specify img extensions you may add as you like
  $extensions= array("jpeg","jpg","png");

  if(in_array($file_ext,$extensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }
  
  //Limit the size of uploaded file 
  if($file_size > 2097152){
     $errors[]='File size must be excately 2 MB';
  }
  
  if(empty($errors)==true){
    // making sure to save our file as webp from XXXX.tmp data format 
     move_uploaded_file($file_tmp,"images/".str_replace($file_ext, 'webp', $file_name));
     echo "Success ";
     
  }else{
     print_r($errors);
  }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 2022-07-07
    • 2012-10-24
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2017-04-24
    相关资源
    最近更新 更多