I am trying to convert a webp file to JPEG using imagecreatefromwebp() but unfortunately, it throws me a warning: Warning: imagecreatefromwebp(): WebP decode: fail to decode input data.
Here's my code
$filename = dirname(__FILE__)."\\".$keyword."1.webp"; // $keyword = 'xyz';
$im = imagecreatefromwebp($filename);
// Convert it to a jpeg file with 100% quality
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);
Please help.
i am using this code, it works fine for me. Here $data contains the base64encoded data
$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);
The imagecreatefromwebp() function accepts either a valid file or URL. You can also pass the your binary data in that function. You can check the function definition and example here http://php.net/manual/en/function.imagecreatefromwebp.php
Related
I am loading an image from a remote server (google for example), but PHP never treats it as an image. This is my code:
$photo = imagecreatefromstring(file_get_contents("https://some.com/image.jpg"));
$filename = $photo["name"];
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); //returns ""
$filepath = $photo["tmp_name"];
is_array(getimagesize($filepath); //returns false
What am I doing wrong?
imagecreatefromstring returns an image not array.
$photo = imagecreatefromstring(file_get_contents("https://some.com/image.jpg"));
ob_end_clean();
header('Content-type: image/jpeg');
imagejpeg($photo, null, 80);
If this code doesn't produce an image then start debugging from finding out the return value of file_get_contents().
Also - after creating an image (using any of imagecreatefrom... functions) you are dealing with image resource, not file system or any other object. Image resource does not have information you are trying to extract (name, pathinfo etc.)
Want to take image from own server rotate certain angle and save the image.
Image file $filename = 'kitten_rotated.jpg'; With echo '<img src='.$filename.'>'; i see the image.
Then
$original = imagecreatefromjpeg($filename);
$angle = 90.0;
$rotated = imagerotate($original, $angle, 0);
Based on this https://stackoverflow.com/a/3693075/2118559 answer trying create image file
$output = 'google.com.jpg';
If i save the same image with new file name, all works
file_put_contents( $output, file_get_contents($filename) );
But if i try to save rotated image, then file_put_contents(): supplied resource is not a valid stream resource.
file_put_contents( $output, $rotated );
Here https://stackoverflow.com/a/12185462/2118559 read $export is going to be a GD image handle. It is NOT something you can simply dump out to a file and expect to get a JPG or PNG image.. but can not understand how to use the code in that answer.
How to create image file from $rotated?
Tried to experiment, based on this http://php.net/manual/en/function.imagecreatefromstring.php
$fh = fopen( 'some_name.png' , 'w') or die("can't open file");
fwrite($fh, $data );
fclose($fh);
Does it means that need something like
$data = base64_encode($rotated);
And then write in new file?
I have not tested this, but I think you need to encode the image as base 64 first.
If you check the string from any Image URL, you'd see data:image/png;base64, preceding the hash. Prepending this to your image string and saving.
Here is a function that may help, based on what you already have:
// Function settings:
// 1) Original file
// 2) Angle to rotate
// 3) Output destination (false will output to browser)
function RotateJpg($filename = '',$angle = 0,$savename = false)
{
// Your original file
$original = imagecreatefromjpeg($filename);
// Rotate
$rotated = imagerotate($original, $angle, 0);
// If you have no destination, save to browser
if($savename == false) {
header('Content-Type: image/jpeg');
imagejpeg($rotated);
}
else
// Save to a directory with a new filename
imagejpeg($rotated,$savename);
// Standard destroy command
imagedestroy($rotated);
}
// Base image
$filename = 'http://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg';
// Destination, including document root (you may have a defined root to use)
$saveto = $_SERVER['DOCUMENT_ROOT']."/images/test.jpg";
// Apply function
RotateJpg($filename,90,$saveto);
If you want to save image just use one of GD library functions: imagepng() or imagepng().
imagerotate() returns image resource so this is not something like string.
In your case just save rotate image:
imagejpg($rotated, $output);
And now You can use $output variable as your new filename to include in view like before:
echo '<img src='.$output.'>';
Don't forget to include appropriate permissions in directory where You're saveing image.
hi guys ive created a base64 encoded image captured with web cam now i convert the .png to .jpg all works fine but now i get two images on server both .png and .jpg how do i go about deleting the .png or is their a way to convert to jpg without saving .png image to disk thanx here my code
$rawData = $_POST['imgBase64'];
$filteredData = explode(',', $rawData);
$unencoded = base64_decode($filteredData[1]);
$randomName = rand(1000, 99999999999);
//Create the image
$fp = fopen('user/'.$randomName.'.png', 'w');
fwrite($fp, $unencoded);
//convert image from png to jpg
$image = imagecreatefrompng('user/'.$randomName.'.png');
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
unlink($fp);
ive tried it with
unlink($image);
unlink($_SERVER['DOCUMENT_ROOT'] . "/user/.$randomName.'.png'");
imagedestroy($fp);
imagedestroy($image);
Use the function unlink() but passing the file name to it instead of the file handler.
So from your example it would be:
EDIT: You might need to close the file first:
fclose( $fp );
unlink( 'user/'.$randomName.'.png' );
as far as i understand all you need is:
$data = base64_decode( $_POST['imgBase64']);
// image resource from your string
$image = imagecreatefromstring($data);
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
I have sent a base64 encoded string via AJAX to PHP and created an image resource with imagecreatefromstring - all is fine.
Now I want to get the base64 encoded string after resizing te image, but i CANT find a function to get the base64encoded string.
Taken from http://www.php.net/manual/en/book.image.php#93393
$image = imagecreatefromstring($file);
// start buffering
ob_start();
imagepng($image);
$contents = ob_get_clean();
echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";
imagedestroy($image);
I am connecting to Active-Directory and getting the thumbnailPhoto attribute successfully.
I have stored the file in the DB using Base64 encoding which makes the result look like:
/9j/4AAQSkZJRgABAQEAYABgAAD/4RHoRXhpZgAATU0AKgAAAAgABQEyAAIAAAAUAA ...
(Full Base64 encoded string: http://pastebin.com/zn2wDEmd)
Using a simple Base64 Decoder and decoding the string into a binary file and rename that to jpeg and open with an image viewer (here: Irfan View) I get the correct picture - see yourself:
How do I achieve this through PHP - I have tried using:
<?php
$data = '/9j/4A...'; //The entire base64 string - gives an error in dreamweaver
$data = base64_decode($data);
$fileTmp = imagecreatefromstring($data);
$newImage = imagecreatefromjpeg($fileTmp);
if (!$newImage) {
echo("<img src=".$newImage."/>");
}
?>
I'm just getting a blank page!
Your problem is that imagecreatefromstring() doesn't return a file, but rather an image in memory that should be output with the correct headers.
$data = base64_decode($data);
// Create image resource from your data string
$imgdata = imagecreatefromstring($data);
if ($imgdata) {
// Send JPEG headers
header("Content-type: image/jpeg");
// Output the image data
imagejpeg($imgdata);
// Clean up the resource
imagedestroy($imgdata);
exit();
}