在上传时将图像文件转换为webp



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

在尝试让这个工作,我有:

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

多次尝试传递数据导致我尝试这样做,所以我可以使用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():参数#1 ($image)必须是gimage类型,字符串给定'。错误发生在第#415行——(参见代码——行有'# <=== = is line 50'来显示错误发生的位置)。寻找解决此错误的帮助。我已经做了好几天了,完全是个新手。

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('xFFxD8xFFxE0x00x10JFIFx00x01x01x01x00...', '/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

从这段代码中获取引用。在这段代码中,您可以看到图像通过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);
}
}

最新更新