| Current Path : /home/echanges/www/modules/mod_galleryfp/plugins/ |
| Current File : /home/echanges/www/modules/mod_galleryfp/plugins/imagemagic.php |
<?php
/*
* GMapFP Component Google Map for Joomla! 3.x
* Version J3.12pro
* Creation date: Décembre 2013
* Author: Fabrice4821 - www.gmapfp.org
* Author email: webmaster@gmapfp.org
* License GNU/GPL
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.filesystem.folder' );
jimport( 'joomla.filesystem.file' );
class GMapFPImageMagic
{
static function imageMagic($fileIn, $fileOut = null, $width = null, $height = null, $crop = null, $typeOut = null, &$errorMsg) {
//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2'))
{
$errorMsg = "GD is not loaded : ".E_USER_WARNING;
return false;
}
// $params = JComponentHelper::getParams('com_gmapfp') ;
// $jfile_thumbs = $params->get( 'gmapfp_jfile_thumbs', 0 ); // a 0 pour Free.fr
// $jpeg_quality = $params->get( 'gmapfp_jpeg_quality', 60 );
$jfile_thumbs = 0;
$jpeg_quality = 80;
$jpeg_quality = self::_getJpegQuality($jpeg_quality);
// Memory - - - - - - - -
$memory = 8;
$memoryLimitChanged = 0;
$memory = (int)ini_get( 'memory_limit' );
if ($memory == 0) {
$memory = 8;
}
// - - - - - - - - - - -
if ($fileIn !== '') {
// array of width, height, IMAGETYPE, "height=x width=x" (string)
list($w, $h, $type) = GetImageSize($fileIn);
if ($w > 0 && $h > 0) {// we got the info from GetImageSize
// size of the image
if ($width == null || $width == 0) { // no width added
$width = $w;
}
else if ($height == null || $height == 0) { // no height, adding the same as width
$height = $width;
}
if ($height == null || $height == 0) { // no height, no width
$height = $h;
}
// miniaturisation
if (!$crop) { // new size - nw, nh (new width/height)
if (false) {
$scale = (($width / $w) < ($height / $h)) ? ($width / $w) : ($height / $h); // smaller rate
} else {
$scale = (($width / $w) > ($height / $h)) ? ($width / $w) : ($height / $h); // greater rate
}
$src = array(0,0, $w, $h);
$dst = array(0,0, floor($w*$scale), floor($h*$scale));
}
else { // will be cropped
$scale = (($width / $w) > ($height / $h)) ? ($width / $w) : ($height / $h); // greater rate
$newW = $width/$scale; // check the size of in file
$newH = $height/$scale;
// which side is larger (rounding error)
if (($w - $newW) > ($h - $newH)) {
$src = array(floor(($w - $newW)/2), 0, floor($newW), $h);
}
else {
$src = array(0, floor(($h - $newH)/2), $w, floor($newH));
}
$dst = array(0,0, floor($width), floor($height));
}
}
if ($memory < 250) {
ini_set('memory_limit', '250M');
$memoryLimitChanged = 1;
}
// Resampling
// in file
switch($type) {
case IMAGETYPE_JPEG:
if (!function_exists('ImageCreateFromJPEG')) {
$errorMsg = 'Error No JPG Function';
return false;
}
$image1 = @ImageCreateFromJPEG($fileIn);
break;
case IMAGETYPE_PNG :
if (!function_exists('ImageCreateFromPNG')) {
$errorMsg = 'Error No PNG Function';
return false;
}
$image1 = @ImageCreateFromPNG($fileIn);
break;
case IMAGETYPE_GIF :
if (!function_exists('ImageCreateFromGIF')) {
$errorMsg = 'Error No GIF Function';
return false;
}
$image1 = @ImageCreateFromGIF($fileIn);
break;
case IMAGETYPE_WBMP:
if (!function_exists('ImageCreateFromWBMP')) {
$errorMsg = 'Error No WBMP Function';
return false;
}
$image1 = @ImageCreateFromWBMP($fileIn);
break;
default:
$errorMsg = $fileIn.' Error Not Supported Type Image : Only JPEG, PNG, GIF and WBMP';
return false;
break;
}
if ($image1) {
$image2 = @ImageCreateTruecolor($dst[2], $dst[3]);
if (!$image2) {
$errorMsg = $fileIn.'Error No Image Create True color';
return false;
}
switch($type) {
case IMAGETYPE_PNG:
@imagealphablending($image2, false);
@imagesavealpha($image2, true);
break;
}
ImageCopyResampled($image2, $image1, $dst[0],$dst[1], $src[0],$src[1], $dst[2],$dst[3], $src[2],$src[3]);
// Create the file
if ($typeOut == null) { // no bitmap
$typeOut = ($type == IMAGETYPE_WBMP) ? IMAGETYPE_PNG : $type;
}
switch($typeOut) {
case IMAGETYPE_JPEG:
if (!function_exists('ImageJPEG')) {
$errorMsg = 'ErrorNoJPGFunction';
return false;
}
if ($jfile_thumbs == 1) {
ob_start();
if (!@ImageJPEG($image2, NULL, $jpeg_quality)) {
ob_end_clean();
$errorMsg = 'ErrorWriteFile jpeg n1';
return false;
}
$imgJPEGToWrite = ob_get_contents();
ob_end_clean();
if(!JFile::write( $fileOut, $imgJPEGToWrite)) {
$errorMsg = 'ErrorWriteFile jpeg n2';
return false;
}
} else {
if (!@ImageJPEG($image2, $fileOut, $jpeg_quality)) {
$errorMsg = 'ErrorWriteFile jpeg n3';
return false;
}
}
break;
case IMAGETYPE_PNG :
if (!function_exists('ImagePNG')) {
$errorMsg = 'ErrorNoPNGFunction';
return false;
}
if ($jfile_thumbs == 1) {
ob_start();
if (!@ImagePNG($image2, NULL)) {
ob_end_clean();
$errorMsg = 'ErrorWriteFile png n1';
return false;
}
$imgPNGToWrite = ob_get_contents();
ob_end_clean();
if(!JFile::write( $fileOut, $imgPNGToWrite)) {
$errorMsg = 'ErrorWriteFile png n2';
return false;
}
} else {
if (!@ImagePNG($image2, $fileOut)) {
$errorMsg = 'ErrorWriteFile png n3';
return false;
}
}
break;
case IMAGETYPE_GIF :
if (!function_exists('ImageGIF')) {
$errorMsg = 'ErrorNoGIFFunction';
return false;
}
if ($jfile_thumbs == 1) {
ob_start();
if (!@ImageGIF($image2, NULL)) {
ob_end_clean();
$errorMsg = 'ErrorWriteFile gif n1';
return false;
}
$imgGIFToWrite = ob_get_contents();
ob_end_clean();
if(!JFile::write( $fileOut, $imgGIFToWrite)) {
$errorMsg = 'ErrorWriteFile gif n2';
return false;
}
} else {
if (!@ImageGIF($image2, $fileOut)) {
$errorMsg = 'ErrorWriteFile gif n3';
return false;
}
}
break;
default:
$errorMsg = 'Error Not Supported Image $typeout';
return false;
break;
}
// free memory
ImageDestroy($image1);
ImageDestroy($image2);
if ($memoryLimitChanged == 1) {
$memoryString = $memory . 'M';
ini_set('memory_limit', $memoryString);
}
$errorMsg = ''; // Success
return true;
} else {
$errorMsg = 'Error1';
return false;
}
if ($memoryLimitChanged == 1) {
$memoryString = $memory . 'M';
ini_set('memory_limit', $memoryString);
}
}
$errorMsg = 'Error2';
return false;
}
static function _getJpegQuality($jpegQuality) {
if ((int)$jpegQuality < 0) {
$jpegQuality = 0;
}
if ((int)$jpegQuality > 100) {
$jpegQuality = 100;
}
return $jpegQuality;
}
}
?>