/** * Rotate the source image and store it to dest path * Return true if successful and false otherwise */ public static function rotate($srcPath, $destPath, $degrees) { $config = CFactory::getConfig(); $app = JFactory::getApplication(); // Set output quality $imgQuality = $config->get('output_image_quality'); $imageEngine = $config->get('imageengine'); $magickPath = $config->get('magickPath'); $info = getimagesize($srcPath); $imgType = image_type_to_mime_type($info[2]); $rotate = null; // Use imageMagick if available if (class_exists('Imagick') && ($imageEngine == 'auto' || $imageEngine == 'imagick')) { //$jconfig = JFactory::getConfig(); //$tmpPath = $jconfig->getValue('config.tmp_path') .'/'. JFile::getName($srcPath); $tmpPath = $app->getCfg('tmp_path') . '/' . JFile::getName($srcPath); $image = new Imagick(); $image->readImage($srcPath); // ImageMagick seems to rotate it counter-clockwise, hence have // to multiply the degress by -1 $image->rotateImage(new ImagickPixel(), $degrees * -1); $image->writeImage($tmpPath); $image->clear(); $image->destroy(); JFile::move($tmpPath, $destPath); return true; } else { if ($imgType == 'image/png' && function_exists('imagecreatefrompng')) { $source = imagecreatefrompng($srcPath); if ($degrees == '90') { $rotatedImage = CImageHelper::rotatePNGImage($source); } else { if ($degrees == '-90') { $rotatedImage = CImageHelper::rotatePNGImage(CImageHelper::rotatePNGImage(CImageHelper::rotatePNGImage($source))); } } ob_start(); imagepng($rotatedImage); $output = ob_get_contents(); ob_end_clean(); // @todo, need to verify that the $output is indeed a proper image data return JFile::write($destPath, $output); } else { if ($imgType == 'image/jpeg' && function_exists('imagecreatefromjpeg') && function_exists('imagerotate')) { // @todo: Support rotation for other image type other than JPEG // Load $source = imagecreatefromjpeg($srcPath); // Rotate $rotate = imagerotate($source, $degrees, 0); if ($rotate) { // Output ob_start(); // We default to use jpeg imagejpeg($rotate, null, $imgQuality); $output = ob_get_contents(); ob_end_clean(); // @todo, need to verify that the $output is indeed a proper image data return JFile::write($destPath, $output); } } } } return false; }