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 createFileThumbnail($fileOriginal, $fileThumbnail, $size, $frontUpload = 0, &$errorMsg) { $paramsC = JComponentHelper::getParams('com_phocagallery'); $enable_thumb_creation = $paramsC->get('enable_thumb_creation', 1); $watermarkParams['create'] = $paramsC->get('create_watermark', 0); // Watermark $watermarkParams['x'] = $paramsC->get('watermark_position_x', 'center'); $watermarkParams['y'] = $paramsC->get('watermark_position_y', 'middle'); $crop_thumbnail = $paramsC->get('crop_thumbnail', 5); // Crop or not $crop = null; switch ($size) { case 'small1': case 'small2': case 'small3': case 'medium1': case 'medium2': case 'medium3': $crop = 1; break; case 'small': if ($crop_thumbnail == 3 || $crop_thumbnail == 5 || $crop_thumbnail == 6 || $crop_thumbnail == 7) { $crop = 1; } break; case 'medium': if ($crop_thumbnail == 2 || $crop_thumbnail == 4 || $crop_thumbnail == 5 || $crop_thumbnail == 7) { $crop = 1; } break; case 'large': default: if ($crop_thumbnail == 1 || $crop_thumbnail == 4 || $crop_thumbnail == 6 || $crop_thumbnail == 7) { $crop = 1; } break; } // disable or enable the thumbnail creation if ($enable_thumb_creation == 1) { $fileResize = PhocaGalleryFileThumbnail::getThumbnailResize($size); if (JFile::exists($fileOriginal)) { //file doesn't exist, create thumbnail if (!JFile::exists($fileThumbnail)) { $errorMsg = 'Error4'; //Don't do thumbnail if the file is smaller (width, height) than the possible thumbnail list($width, $height) = GetImageSize($fileOriginal); //larger phocagalleryimport('phocagallery.image.imagemagic'); if ($width > $fileResize['width'] || $height > $fileResize['height']) { $imageMagic = PhocaGalleryImageMagic::imageMagic($fileOriginal, $fileThumbnail, $fileResize['width'], $fileResize['height'], $crop, null, $watermarkParams, $frontUpload, $errorMsg); } else { $imageMagic = PhocaGalleryImageMagic::imageMagic($fileOriginal, $fileThumbnail, $width, $height, $crop, null, $watermarkParams, $frontUpload, $errorMsg); } if ($imageMagic) { return true; } else { return false; // error Msg will be taken from imageMagic } } else { $errorMsg = 'ThumbnailExists'; //thumbnail exists return false; } } else { $errorMsg = 'ErrorFileOriginalNotExists'; return false; } $errorMsg = 'Error3'; return false; } else { $errorMsg = 'DisabledThumbCreation'; // User have disabled the thumbanil creation e.g. because of error return false; } }