To convert image by image magic in php - php

I have to convert a gif image to png image. For which i have tried this. The image file is in folder ( localhost ).
I have tried the below code.
$imageFile = "14981180607.gif";
$temp = sys_get_temp_dir();
file_put_contents($temp, file_get_contents($imageFile) );
exec("convert ".escapeshellarg($temp)." img/xx_%05d.png");
I need the image to be converted and store in my img folder, which i mentioned the path.

Related

How to save an image in upload folder from bytes string of image?

I want to save an image in particular folder. here i am using image bytes string to upload. i am using "imagecreatefromstring($bytesarray)" for this. and "imagejpeg()" which is given me a jpeg image which i passed in imagecreatefromstring() function.
here i am using file_put_contents() for uploading an image. but the image still not save in folder.
Here below is my code.
$imgdata = base64_decode($fl_data[$i]['Archivo']);
$im = imagecreatefromstring($imgdata);
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
$filemainpath = base_url().'uploads/apifiles/'.$filename.".".$extension;
file_put_contents($filemainpath, $im);
give the folder permission to 777 and use absolute path instead of base_url()
$filemainpath = __dir__.'../uploads/apifiles/'.$filename.".".$extension;
You can also use DOCUMENT_ROOT:
$filemainpath = $_SERVER['DOCUMENT_ROOT'] . "/YourAppName/uploads/apifiles/".$filename.".".$extension;

How to get image name automatically from destination folder

I am working on a project that resize images. My following code takes an image from upload directory, resizes it and save the output image but the problem is that I have to hard code image name.
I want to get image name automatically from upload directory. Please someone solve my problem.
<?php
include('resize_lib.php'); // resize_lib is the library that has functionality of how to resize the image
//focus on this line
$image_path = "upload/something.jpg";// hard coded image name
$resizeObj = new resize($image_path);
$resizeObj -> resizeImage(1536, 1024, 0); // width // height
$resizeObj -> saveImage("new.png", 100);
echo "done...";
?>
Try to get all images files from your uploaded directory .jpg or .png or .gif
$files = glob("upload/*.{jpg,png,gif}", GLOB_BRACE);
glob
Returns an array containing the matched files/directories, an empty
array if no file matched or FALSE on error.
You can then use foreach() loop to set your image name for $image_path. By the way you can also select only a single type of image e.g something.jpg
$files = glob('upload/*.jpg');

How do I convert images from base 64 to their file types in PHP?

I have objects containing images as base 64 strings, the object also contains file names for the images, and file types (jpeg, png, gif & bmp) for the images.
The base 64 strings have already had the tags (e.g. "data:image/png;base64" removed from the beginning.
The format for the objects ($myImg) are as follows:
$myImg->fileName contains the name that the converted image should be saved under.
$myImg->fileType describes the format that the file is supposed to be saved as - this is used to specify the path extension in the fopen() function.
$myImg->b64 contains the 64 bit binary string which represents the image.
The code for my function is as folows:
function toImg(ImageString $myImg){
//Output file is in the same directory as the PHP script.
//Uses the object's filetype attribute as the file extension.
$outputFile = fopen($myImg->fileName . "." . $myImg->fileType, "w");
$image = base64_decode($myImg->b64);
fwrite($outputFile, $image);
fclose($outputFile);
}
The function creates the image files, but I get errors when trying to view them in Xubuntu Image Viewer. The errors are as follows:
Error interpreting JPEG image file (Not a JPEG file: starts with 0x14 0x00)
Fatal error reading PNG image file: not a PNG file.
File does not appear to be a GIF file.
BMP image has bogus header data.
I've looked through and followed guides for base64 to image conversion, but none of them have encountered these errors.
Try to show the image inline in the browser, like this:
<img src="data:image/png;base64,the-base64-string" />
(change png to the correct image format)
If the image is still broken, then the image data is invalid.
You can decode the image from base64 like this:
function base64_to_jpeg_img($base64_img_string, $output_img) {
$input_file_open = fopen($output_img, "wb");
$data = explode(',', $base64_img_string);
fwrite($input_file_open, base64_decode($data[1]));
fclose($input_file_open);
return $output_img;
}
Hope this helps!

Save all uploaded images (png, gif ) as jpg using php

How i can save all uploaded images as jpg using php.
i have code php to upload images like this
$upload_dir="../uploads/";
$filename = $_FILES['pic1']['name'];
$tmp_name=$_FILES['pic1']['tmp_name'];
$path=$upload_dir.$filename;
move_uploaded_file($tmp_name, $path);
i am using
$image_path=imagecreatefromjpeg($path);
`imagejpeg($image_path);`
but not work!.
This won't work because you tell PHP to create image from jpeg format and your uploaded file is gif or png. You could use imagecreatefromstring() function:
$image_path = imagecreatefromstring(file_get_contents($path));
imagejpeg($image_path);

Disable clipping path from EPS with Imagick

I have an EPS file from Photoshop with a clipping path on it:
EPS File
I need to get a thumbnail from this file, so I have the following code:
$img = new imagick();
$img->readimage($uploadfile);
$img->trimimage();
$img->setImageFormat("jpeg");
$img->scaleImage(200,200);
$img->writeimage($uploaddir . $thumb . ".jpg");
I get the following:
Thumb bad
Instead of what I need:
Thumb ok
If I open the file on Photoshop and remove the path by hand I can see it ok, but I have around 3000 images like this, so I need to do this automatically.

Categories