Skip to content

Instantly share code, notes, and snippets.

@MikeWilkie
Created October 30, 2014 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MikeWilkie/7f60f7a0e6f9b9f94058 to your computer and use it in GitHub Desktop.
Save MikeWilkie/7f60f7a0e6f9b9f94058 to your computer and use it in GitHub Desktop.
Magento png to jpg
<?php
require_once '../app/Mage.php';
umask(0);
Mage::app('');
function dupeImage($path) {
$img = imagecreatefrompng($path);
$new = str_replace('.png','.jpg',$path);
$bg = imagecreatetruecolor(imagesx($img), imagesy($img));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
$quality = 100; // 0 = worst / smaller file, 100 = better / bigger file
imagejpeg($bg, $new, $quality);
ImageDestroy($bg);
echo $new . "\n";
}
$magePath = Mage::getBaseDir();
$mediaPath = $magePath . '/media/catalog/product';
$cachePath = $mediaPath . '/cache';
$directory = new RecursiveDirectoryIterator($mediaPath,RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directory,RecursiveIteratorIterator::LEAVES_ONLY);
$extensions = array("png");
foreach ($iterator as $fileinfo) {
if (in_array($fileinfo->getExtension(), $extensions)) {
if (strpos($fileinfo->getPathname(),$cachePath) !== true) {
$files[] = $fileinfo->getPathname();
}
}
}
foreach ($files as $galleryImage){
dupeImage($galleryImage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment