'Convert image file to webp on upload
Trying to write script which will convert image type (gif, jpeg, jpg,png) to webP in addition to uploading original file for fallback in browsers not supporting webP (earlier versions of Safari, etc.)
In attempting to get this to work I have:
- built a script which takes all image types and uploads them successfully to server, saving three different sized thumbnails for various purpose.
- enable gd library on my local mamp server (6.4) running on OS X/Mac
- Read a great deal about webP
- attempted several tutorials / disceted a bunch of examples
- attempted to research my error - found little reference on web that was useful
Multiple attempts to hand the data off have lead me to attempt to do so as a trying so I can use the imagewebp($imgWEBP, $uploadPATH) to convert & upload the image to the server from the local host/desktop of the user to the site server image directory. Other attempts to convert the image using a more direct handoff $_FILES['image']['tmp_name'], $_FILES['image']['name'] have failed; Currently I'm trying to save the image in a folder in same directory as script, I'll re-path later once script is working properly.
I believe the reoccurring problem causing the error is in the handoff of the image source/data so the image data can be converted to webP and uploaded to server.
The tutorials and examples i have found dont' address how to hand the data off in an acceptable way, they all start with the data as being in that form an don't tell you what the form it.
I have gotten very far in this script, it's my most advanced script I've tried to do so far. First time trying to use GD library. I am a novice and probably way over my head, but trying, i'm hoping that I can get the proper guidance to solve the error type issue (Type must be GdImage, string given)
PHP SCRIPT
# 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
To summarize, problem is in a nutshell, attempt to hand off image file to imagewebP function fails. All attempts produce the same error 'Uncaught TypeError: imagewebp(): Argument #1 ($image) must be of type GdImage, string given'. Error happens on line #415 -- (SEE code -- line has '# <=== is line 50' to call out where error happens). Looking for help resolving this error. Been working on this for several days - total newb here.
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
Solution 1:[1]
Take Reference from this code. In this code as you can see image is received by post method and then simply saved as .webp also considering each type of image formats. What is happening here ?
The image is stored directly as webp from tmp file format so the size of both will also be same
//Image is sanded by POST Method
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Deepak Chauhan |
