Exemple #1
0
 public static function rotateImage($thumbName, $size, $angle = 90, &$errorMsg)
 {
     $params = JComponentHelper::getParams('com_phocagallery');
     $jfile_thumbs = $params->get('jfile_thumbs', 1);
     $jpeg_quality = $params->get('jpeg_quality', 85);
     $jpeg_quality = PhocaGalleryImage::getJpegQuality($jpeg_quality);
     // Try to change the size
     $memory = 8;
     $memoryLimitChanged = 0;
     $memory = (int) ini_get('memory_limit');
     if ($memory == 0) {
         $memory = 8;
     }
     $fileIn = $thumbName->abs;
     $fileOut = $thumbName->abs;
     if ($fileIn !== '' && file_exists($fileIn)) {
         //array of width, height, IMAGETYPE, "height=x width=x" (string)
         list($w, $h, $type) = GetImageSize($fileIn);
         // we got the info from GetImageSize
         if ($w > 0 && $h > 0 && $type != '') {
             // Change the $w against $h because of rotating
             $src = array(0, 0, $w, $h);
             $dst = array(0, 0, $h, $w);
         } else {
             $errorMsg = 'ErrorWorHorType';
             return false;
         }
         // Try to increase memory
         if ($memory < 50) {
             ini_set('memory_limit', '50M');
             $memoryLimitChanged = 1;
         }
         switch ($type) {
             case IMAGETYPE_JPEG:
                 if (!function_exists('ImageCreateFromJPEG')) {
                     $errorMsg = 'ErrorNoJPGFunction';
                     return false;
                 }
                 //$image1 = ImageCreateFromJPEG($fileIn);
                 try {
                     $image1 = ImageCreateFromJPEG($fileIn);
                 } catch (\Exception $exception) {
                     $errorMsg = 'ErrorJPGFunction';
                     return false;
                 }
                 break;
             case IMAGETYPE_PNG:
                 if (!function_exists('ImageCreateFromPNG')) {
                     $errorMsg = 'ErrorNoPNGFunction';
                     return false;
                 }
                 //$image1 = ImageCreateFromPNG($fileIn);
                 try {
                     $image1 = ImageCreateFromPNG($fileIn);
                 } catch (\Exception $exception) {
                     $errorMsg = 'ErrorPNGFunction';
                     return false;
                 }
                 break;
             case IMAGETYPE_GIF:
                 if (!function_exists('ImageCreateFromGIF')) {
                     $errorMsg = 'ErrorNoGIFFunction';
                     return false;
                 }
                 //$image1 = ImageCreateFromGIF($fileIn);
                 try {
                     $image1 = ImageCreateFromGIF($fileIn);
                 } catch (\Exception $exception) {
                     $errorMsg = 'ErrorGIFFunction';
                     return false;
                 }
                 break;
             case IMAGETYPE_WBMP:
                 if (!function_exists('ImageCreateFromWBMP')) {
                     $errorMsg = 'ErrorNoWBMPFunction';
                     return false;
                 }
                 //$image1 = ImageCreateFromWBMP($fileIn);
                 try {
                     $image1 = ImageCreateFromWBMP($fileIn);
                 } catch (\Exception $exception) {
                     $errorMsg = 'ErrorWBMPFunction';
                     return false;
                 }
                 break;
             default:
                 $errorMsg = 'ErrorNotSupportedImage';
                 return false;
                 break;
         }
         if ($image1) {
             // Building image for ROTATING
             /*	$image2 = @ImageCreateTruecolor($dst[2], $dst[3]);
             			if (!$image2) {
             				return 'ErrorNoImageCreateTruecolor';
             			}*/
             /*	if(!function_exists("imagerotate")) {
             				$errorMsg = 'ErrorNoImageRotate';
             				return false;
             			}*/
             switch ($type) {
                 case IMAGETYPE_PNG:
                     //	imagealphablending($image1, false);
                     //	imagesavealpha($image1, true);
                     if (!function_exists("imagecolorallocate")) {
                         $errorMsg = 'ErrorNoImageColorAllocate';
                         return false;
                     }
                     if (!function_exists("imagefill")) {
                         $errorMsg = 'ErrorNoImageFill';
                         return false;
                     }
                     if (!function_exists("imagecolortransparent")) {
                         $errorMsg = 'ErrorNoImageColorTransparent';
                         return false;
                     }
                     $colBlack = imagecolorallocate($image1, 0, 0, 0);
                     if (!function_exists("imagerotate")) {
                         $image2 = PhocaGalleryImageRotate::imageRotate($image1, $angle, $colBlack);
                     } else {
                         $image2 = imagerotate($image1, $angle, $colBlack);
                     }
                     imagefill($image2, 0, 0, $colBlack);
                     imagecolortransparent($image2, $colBlack);
                     break;
                 default:
                     if (!function_exists("imagerotate")) {
                         $image2 = PhocaGalleryImageRotate::imageRotate($image1, $angle, 0);
                     } else {
                         $image2 = imageRotate($image1, $angle, 0);
                     }
                     break;
             }
             // Get the image size and resize the rotated image if necessary
             $rotateWidth = imagesx($image2);
             // Get the size from rotated image
             $rotateHeight = imagesy($image2);
             // Get the size from rotated image
             $parameterSize = PhocaGalleryFileThumbnail::getThumbnailResize($size);
             $newWidth = $parameterSize['width'];
             // Get maximum sizes, they can be displayed
             $newHeight = $parameterSize['height'];
             // Get maximum sizes, they can be displayed
             $scale = $newWidth / $rotateWidth < $newHeight / $rotateHeight ? $newWidth / $rotateWidth : $newHeight / $rotateHeight;
             // smaller rate
             $src = array(0, 0, $rotateWidth, $rotateHeight);
             $dst = array(0, 0, floor($rotateWidth * $scale), floor($rotateHeight * $scale));
             // If original is smaller than thumbnail size, don't resize it
             if ($src[2] > $dst[2] || $src[3] > $dst[3]) {
                 // Building image for RESIZING THE ROTATED IMAGE
                 $image3 = @ImageCreateTruecolor($dst[2], $dst[3]);
                 if (!$image3) {
                     $errorMsg = 'ErrorNoImageCreateTruecolor';
                     return false;
                 }
                 ImageCopyResampled($image3, $image2, $dst[0], $dst[1], $src[0], $src[1], $dst[2], $dst[3], $src[2], $src[3]);
                 switch ($type) {
                     case IMAGETYPE_PNG:
                         //	imagealphablending($image2, true);
                         //	imagesavealpha($image2, true);
                         if (!function_exists("imagecolorallocate")) {
                             $errorMsg = 'ErrorNoImageColorAllocate';
                             return false;
                         }
                         if (!function_exists("imagefill")) {
                             $errorMsg = 'ErrorNoImageFill';
                             return false;
                         }
                         if (!function_exists("imagecolortransparent")) {
                             $errorMsg = 'ErrorNoImageColorTransparent';
                             return false;
                         }
                         $colBlack = imagecolorallocate($image3, 0, 0, 0);
                         imagefill($image3, 0, 0, $colBlack);
                         imagecolortransparent($image3, $colBlack);
                         break;
                 }
             } else {
                 $image3 = $image2;
             }
             switch ($type) {
                 case IMAGETYPE_JPEG:
                     if (!function_exists('ImageJPEG')) {
                         $errorMsg = 'ErrorNoJPGFunction';
                         return false;
                     }
                     if ($jfile_thumbs == 1) {
                         ob_start();
                         if (!@ImageJPEG($image3, NULL, $jpeg_quality)) {
                             ob_end_clean();
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                         $imgJPEGToWrite = ob_get_contents();
                         ob_end_clean();
                         if (!JFile::write($fileOut, $imgJPEGToWrite)) {
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                     } else {
                         if (!@ImageJPEG($image3, $fileOut, $jpeg_quality)) {
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                     }
                     break;
                 case IMAGETYPE_PNG:
                     if (!function_exists('ImagePNG')) {
                         $errorMsg = 'ErrorNoPNGFunction';
                         return false;
                     }
                     if ($jfile_thumbs == 1) {
                         ob_start();
                         if (!@ImagePNG($image3, NULL)) {
                             ob_end_clean();
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                         $imgPNGToWrite = ob_get_contents();
                         ob_end_clean();
                         if (!JFile::write($fileOut, $imgPNGToWrite)) {
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                     } else {
                         if (!@ImagePNG($image3, $fileOut)) {
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                     }
                     break;
                 case IMAGETYPE_GIF:
                     if (!function_exists('ImageGIF')) {
                         $errorMsg = 'ErrorNoGIFFunction';
                         return false;
                     }
                     if ($jfile_thumbs == 1) {
                         ob_start();
                         if (!@ImageGIF($image3, NULL)) {
                             ob_end_clean();
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                         $imgGIFToWrite = ob_get_contents();
                         ob_end_clean();
                         if (!JFile::write($fileOut, $imgGIFToWrite)) {
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                     } else {
                         if (!@ImageGIF($image3, $fileOut)) {
                             $errorMsg = 'ErrorWriteFile';
                             return false;
                         }
                     }
                     break;
                 default:
                     $errorMsg = 'ErrorNotSupportedImage';
                     return false;
                     break;
             }
             // free memory
             ImageDestroy($image1);
             // Original
             ImageDestroy($image2);
             // Rotated
             ImageDestroy($image3);
             // Resized
             if ($memoryLimitChanged == 1) {
                 $memoryString = $memory . 'M';
                 ini_set('memory_limit', $memoryString);
             }
             return true;
             // Success
         } else {
             $errorMsg = PhocaGalleryUtils::setMessage($errorMsg, JText::_('COM_PHOCAGALLERY_ERROR_IMAGE_NOT_PROCESS'));
             return false;
         }
         if ($memoryLimitChanged == 1) {
             $memoryString = $memory . 'M';
             ini_set('memory_limit', $memoryString);
         }
     }
     $errorMsg = JText::_('COM_PHOCAGALLERY_FILEORIGINAL_NOT_EXISTS');
     return false;
 }
 function rotate($id, $angle, &$errorMsg)
 {
     phocagalleryimport('phocagallery.image.imagerotate');
     if ($id > 0 && $angle != '') {
         $query = 'SELECT a.filename as filename' . ' FROM #__phocagallery AS a' . ' WHERE a.id = ' . (int) $id;
         $this->_db->setQuery($query);
         $file = $this->_db->loadObject();
         if (isset($file->filename) && $file->filename != '') {
             $thumbNameL = PhocaGalleryFileThumbnail::getThumbnailName($file->filename, 'large');
             $thumbNameM = PhocaGalleryFileThumbnail::getThumbnailName($file->filename, 'medium');
             $thumbNameS = PhocaGalleryFileThumbnail::getThumbnailName($file->filename, 'small');
             $errorMsg = $errorMsgS = $errorMsgM = $errorMsgL = '';
             PhocaGalleryImageRotate::rotateImage($thumbNameL, 'large', $angle, $errorMsgS);
             if ($errorMsgS != '') {
                 $errorMsg = $errorMsgS;
                 return false;
             }
             PhocaGalleryImageRotate::rotateImage($thumbNameM, 'medium', $angle, $errorMsgM);
             if ($errorMsgM != '') {
                 $errorMsg = $errorMsgM;
                 return false;
             }
             PhocaGalleryImageRotate::rotateImage($thumbNameS, 'small', $angle, $errorMsgL);
             if ($errorMsgL != '') {
                 $errorMsg = $errorMsgL;
                 return false;
             }
             if ($errorMsgL == '' && $errorMsgM == '' && $errorMsgS == '') {
                 return true;
             } else {
                 $errorMsg = ' (' . $errorMsg . ')';
                 return false;
             }
         }
         $errorMsg = JText::_('COM_PHOCAGALLERY_FILENAME_NOT_EXISTS');
         return false;
     }
     $errorMsg = JText::_('COM_PHOCAGALLERY_ERROR_ITEM_NOT_SELECTED');
     return false;
 }