How can I convert all images to jpg?
Try this code: originalImage
is the path of... the original image... outputImage
is self explaining enough. Quality
is a number from 0 to 100 setting the output jpg quality (0 - worst, 100 - best)
function convertImage($originalImage, $outputImage, $quality)
{
// jpg, png, gif or bmp?
$exploded = explode('.',$originalImage);
$ext = $exploded[count($exploded) - 1];
if (preg_match('/jpg|jpeg/i',$ext))
$imageTmp=imagecreatefromjpeg($originalImage);
else if (preg_match('/png/i',$ext))
$imageTmp=imagecreatefrompng($originalImage);
else if (preg_match('/gif/i',$ext))
$imageTmp=imagecreatefromgif($originalImage);
else if (preg_match('/bmp/i',$ext))
$imageTmp=imagecreatefrombmp($originalImage);
else
return 0;
// quality is a value from 0 (worst) to 100 (best)
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);
return 1;
}
How to convert all images to JPG format in PHP?
Maybe it's not working with PNG because PNG only supports compression levels 0 to 9.
I'd also rather modify the behaviour based on MIME type, not extension. And I guess you're checking your POST user input before using it in code ;)
Here's my variant of the code:
$path = "../images/DVDs/";
$img = $path . $_POST['logo_file'];
if (($img_info = getimagesize($img)) === FALSE)
die("Image not found or not an image");
switch ($img_info[2]) {
case IMAGETYPE_GIF : $src = imagecreatefromgif($img); break;
case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break;
case IMAGETYPE_PNG : $src = imagecreatefrompng($img); break;
default : die("Unknown filetype");
}
$tmp = imagecreatetruecolor(350, 494);
imagecopyresampled($tmp, $src, 0, 0, intval($_POST['x']), intval($_POST['y']),
350, 494, intval($_POST['w']), intval($_POST['h']));
$thumb = $path . pathinfo($img, PATHINFO_FILENAME) . "_thumb";
switch ($img_info[2]) {
case IMAGETYPE_GIF : imagegif($tmp, $thumb . '.gif'); break;
case IMAGETYPE_JPEG : imagejpeg($tmp, $thumb . '.jpg', 100); break;
case IMAGETYPE_PNG : imagepng($tmp, $thumb . '.png', 9); break;
default : die("Unknown filetype");
}
For every filetype you want supported, you only have to add two lines of code.
How can I convert uploaded images to JPG format
Try this:
<?php
$path = "uploads/";
$img = $_FILES['photoimg']['tmp_name'];
$dst = $path . $_FILES['photoimg']['name'];
if (($img_info = getimagesize($img)) === FALSE)
die("Image not found or not an image");
$width = $img_info[0];
$height = $img_info[1];
switch ($img_info[2]) {
case IMAGETYPE_GIF : $src = imagecreatefromgif($img); break;
case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break;
case IMAGETYPE_PNG : $src = imagecreatefrompng($img); break;
default : die("Unknown filetype");
}
$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($tmp, $dst.".jpg");
?>
Converting Image Files to JPEG
In PHP you start off by reading the content of the uploaded files into imagecreatefromstring()
with something like file_get_content($filePath)
. From there you use one imagejpeg
to write to another file with extension .jpg
Here is a sample:
$handle = imagecreatefromstring(file_get_content($filePath));
imagejpeg($handle,'newfile.jpg',100);
Reference
- imagejpeg
- imagecreatefromstring
- file_get_content
PHP - convert all images to jpg using Imagick - bad quality
The image is not in "bad quality" (there is no blurry areas found), but the difference between 2 images is caused by transparent PNG to JPG conversion.
Before you crop the image, add these two lines:
// set background to white (Imagick doesn't know how to deal with transparent background if you don't instruct it)
$img->setImageBackgroundColor(new ImagickPixel('white'));
// flattens multiple layers
$img = $img->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
laravel and image intervention, how to convert all images to jpeg and save from Input::file?
I think you need to call encode()
before you call save()
as save actually saves the image to disk.
$jpg = (string) Image::make('public/foo.png')->encode('jpg', 75);
Documentation: http://image.intervention.io/api/encode
How to write a PHP file converter?
You can get the data from a file, in this case an image by using file_get_contents:
$data = file_get_contents("img.png");
Once you have the data to form the new file, you can then use the function file_put_contents to write your new file:
if (file_put_contents("img.jpg", $data)) {
echo("success");
} else {
echo("failure");
}
file_put_contents returns an int of 0 or 1 so you can determine if it was successful in creating the file.
You could then create the following function:
function imagepng($input, $output) {
return file_put_contents($output, $input);
}
Hope this is helpful.
Related Topics
PHP Warning: Exec() Unable to Fork
How to Select Multiple Rows from MySQL with One Query and Use Them in PHP
PHP Run Once and Insert Twice in MySQL Database
Get the Hierarchy of a Directory with PHP
Using Fetch_Assoc on Prepared Statements
Mod_Rewrite, PHP and the .Htaccess File
How to Install Composer on App Service
Disable Add to Cart Button for an Array of Products Ids in Woocommerce
How to Pass Multiple Variables Across Multiple Pages
PHP Sample Script for Pagination
Array_Map Function in PHP with Parameter
Foreach Value from Post from Form