Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active August 6, 2023 09:29
Show Gist options
  • Save code-boxx/ec4b1deb1a7a0917007afb641f4ede83 to your computer and use it in GitHub Desktop.
Save code-boxx/ec4b1deb1a7a0917007afb641f4ede83 to your computer and use it in GitHub Desktop.
PHP Convert Image File Format

CONVERT IMAGE FILE FORMAT IN PHP

NOTES

Please make sure that extension = gd is enabled in php.ini.

USE THESE IMAGES FOR TESTING IF YOU WANT

ONE TWO THREE

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) IMAGE CONVERT FUNCTION
function imgcon ($from, $to, $quality=null) {
// (A1) CHECK VALID IMAGE TYPE
$extf = strtolower(pathinfo($from, PATHINFO_EXTENSION));
$extt = strtolower(pathinfo($to, PATHINFO_EXTENSION));
$valid = ["bmp", "jpg", "jpeg", "png", "gif", "webp"];
if (!in_array($extf, $valid) || !in_array($extt, $valid)) { return false; }
// (A2) PROCESS CONVERSION
if ($extf=="jpg") { $extf = "jpeg"; }
if ($extt=="jpg") { $extt = "jpeg"; }
$fnf = "imagecreatefrom$extf";
$fnt = "image$extt";
$img = $fnf($from);
imagepalettetotruecolor($img);
if ($quality !== null) { $fnt($img, $to, $quality); } else { $fnt($img, $to); }
imagedestroy($img);
// unlink($from); // delete source image if you want
return true;
}
// (B) GO!
echo imgcon("ONE.jpg", "DEMO.webp") ? "OK" : "ERROR" ;
<?php
// (A) SETTINGS
$cdir = __DIR__ . DIRECTORY_SEPARATOR; // convert all images in this folder
$cto = "webp"; // convert to this format
$cqy = null; // convert quality, null to use default
// (B) CONVERT IMAGE FILES
$clist = array_diff(["bmp", "jpg", "jpeg", "png", "gif", "webp"], [$cto]);
$clist = "$cdir*.{" . implode(",", $clist) . "}";
foreach (glob($clist, GLOB_BRACE) as $file) {
// (B1) "SAVE AS" FILE NAME
$saveas = $cdir . pathinfo($file, PATHINFO_FILENAME) . ".$cto";
if (file_exists($saveas)) { continue; } // do not overwrite
// (B2) PROCESS CONVERSION
$extf = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($extf=="jpg") { $extf = "jpeg"; }
$extt = $cto=="jpg" ? "jpeg" : $cto ;
$fnf = "imagecreatefrom$extf";
$fnt = "image$extt";
$img = $fnf($file);
imagepalettetotruecolor($img);
if ($cqy !== null) { $fnt($img, $saveas, $cqy); }
else { $fnt($img, $saveas); }
imagedestroy($img);
// unlink($file); // delete source image if you want
echo "$saveas - OK";
}
<!DOCTYPE html>
<html>
<head>
<title>Upload Convert Image Demo</title>
<meta charset="utf-8">
</head>
<body>
<form action="3b-upload.php" enctype="multipart/form-data" method="post" target="_blank">
<input type="file" name="up" required accept=".bmp,.jpg,.jpeg,.png,.gif,.webp">
<input type="submit" value="Upload">
</form>
</body>
</html>
<?php
// (A) SETTINGS
$cdir = __DIR__ . DIRECTORY_SEPARATOR; // save to this directory
$cto = "webp"; // convert to this format
$cqy = null; // convert quality, null to use default
// (B) CHECK UPLOADED FILE
$ext = strtolower(pathinfo($_FILES["up"]["name"], PATHINFO_EXTENSION));
if (!in_array($ext, ["bmp", "jpg", "jpeg", "png", "gif", "webp"])) { exit("INVALID IMAGE"); }
// (C) MOVE UPLOADED FILE
$saveto = $cdir . $_FILES["up"]["name"];
move_uploaded_file($_FILES["up"]["tmp_name"], $saveto);
// (D) CONVERT IF NEEDED
if ($ext != $cto) {
$extf = $ext=="jpg" ? "jpeg" : $ext ;
$extt = $cto=="jpg" ? "jpeg" : $cto ;
$saveas = $cdir . pathinfo($saveto, PATHINFO_FILENAME) . ".$cto";
$fnf = "imagecreatefrom$extf";
$fnt = "image$extt";
$img = $fnf($saveto);
imagepalettetotruecolor($img);
if ($cqy !== null) { $fnt($img, $saveas, $cqy); }
else { $fnt($img, $saveas); }
imagedestroy($img);
// unlink($saveto); // delete source image if you want
}
// (E) WHAT'S NEXT?
// REDIRECT TO ANOTHER PAGE?
// SHOW AN "UPLOAD OK PAGE"?
// SIMPLE MESSAGE?
echo "OK";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment