Thumbnail and Resize Image with Media Manager :: Joomla 1.0.4
Posted: Tue Dec 20, 2005 2:08 am
Hi!
I did a small hack in the back-end of media manager. This hack creates a thumbnail of the original image and also resize the image to a specific width. Im using GD2 not imageMagick, also is cofigurable to be used with GD1.
If you want to try this small hack, back-up and edit the file:
/administrator/components/com_media/admin.media.php
Replace the function:
function do_upload($file, $dest_dir)
and add at top of function do_upload two new functions:
/* --------------------------------------------------------------------------- */
// makes a thumbnail using the GD library
function make_thumb_gd ($input_file_name, $input_file_path, $jpegQuality = 70, $thumbnailWidth = 100, $gdVersionTwo = True )
{
$quality = $jpegQuality;
// first, grab the dimensions of the pic
$imagedata = GetImageSize($input_file_path.'/'.$input_file_name);
$imagewidth = $imagedata[0];
$imageheight = $imagedata[1];
$imagetype = $imagedata[2];
// type definitions
// 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP
// 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order)
// 9 = JPC, 10 = JP2, 11 = JPX
$tempFileExt = substr($input_file_name, -4);
$tempFileName = $input_file_name;
$tempFileName = preg_replace('/' . $tempFileExt . '$/', '', $tempFileName);
$tempFileName = strtr($tempFileName, ".", "_");
$tempFileName = preg_replace('/_+/', '_', $tempFileName);
$tempFileName = preg_replace('/_+$/', '', $tempFileName);
$thumb_name = $tempFileName . '_thumb_' . $tempFileExt;
// the GD library, which this uses, can only resize GIF, JPG and PNG
if ($imagetype == 1)
{
// it's a GIF
// figure out the ratio to which it should be shrunk, if at all
// see if GIF support is enabled
if (imagetypes() & IMG_GIF)
{
$shrinkage = 1;
if ($imagewidth > $thumbnailWidth)
{
$shrinkage = $thumbnailWidth/$imagewidth;
}
$dest_height = $shrinkage * $imageheight;
$dest_width = $thumbnailWidth;
$src_img = imagecreatefromgif("$input_file_path/$input_file_name");
$dst_img = imageCreate($dest_width, $dest_height);
//copy the original image info into the new image with new dimensions
//checking to see which function is available
ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagegif($dst_img, "$input_file_path/$thumb_name");
mosChmod("$input_file_path/$thumb_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if GIF support is enabled
} // end if $imagetype == 1
elseif ($imagetype == 2)
{
// it's a JPG
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
if ($imagewidth > $thumbnailWidth)
{
$shrinkage = $thumbnailWidth/$imagewidth;
}
$dest_height = $shrinkage * $imageheight;
$dest_width = $thumbnailWidth;
$src_img = imagecreatefromjpeg("$input_file_path/$input_file_name");
if ($gdVersionTwo == False)
{
$dst_img = imageCreate($dest_width, $dest_height);
}
else
{
$dst_img = imageCreateTrueColor($dest_width, $dest_height);
}
//copy the original image info into the new image with new dimensions
//checking to see which function is available
if ($gdVersionTwo == False)
{
ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
}
else
{
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
}
imagejpeg($dst_img, "$input_file_path/$thumb_name", $quality);
mosChmod("$input_file_path/$thumb_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if $imagetype == 2
elseif ($imagetype == 3)
{
// it's a PNG
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
if ($imagewidth > $thumbnailWidth)
{
$shrinkage = $thumbnailWidth/$imagewidth;
}
$dest_height = $shrinkage * $imageheight;
$dest_width = $thumbnailWidth;
$src_img = imagecreatefrompng("$input_file_path/$input_file_name");
$dst_img = imagecreate($dest_width,$dest_height);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_width,$dest_height, $imagewidth, $imageheight);
imagepng($dst_img, "$input_file_path/$thumb_name");
mosChmod("$input_file_path/$thumb_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if $imagetype == 3
return $thumb_name;
} // end function make_thumb_gd
// resizes image using the GD library
function resize_img_gd ($input_file_name, $input_file_path, $jpegQuality = 70, $maxWidth = 400, $imageWidth = 400, $gdVersionTwo = True)
{
$quality = $jpegQuality;
$max_width = $maxWidth;
// first, grab the dimensions of the pic
$imagedata = getimagesize($input_file_path.'/'.$input_file_name);
$imagewidth = $imagedata[0];
$imageheight = $imagedata[1];
$imagetype = $imagedata[2];
// type definitions
// 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP
// 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order)
// 9 = JPC, 10 = JP2, 11 = JPX
$img_name = $input_file_name; //by default
// the GD library, which this uses, can only resize GIF, JPG and PNG
if ($imagetype == 1)
{
// it's a GIF
// see if GIF support is enabled
if (imagetypes() & IMG_GIF)
{
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
$shrinkage = $max_width/$imagewidth;
$dest_height = $shrinkage * $imageheight;
$dest_width = $max_width;
$src_img = imagecreatefromgif("$input_file_path/$input_file_name");
$dst_img = imageCreate($dest_width, $dest_height);
//copy the original image info into the new image with new dimensions
//checking to see which function is available
ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagegif($dst_img, "$input_file_path/$img_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} //end if GIF support is enabled
} // end if $imagetype == 1
elseif ($imagetype == 2)
{
// it's a JPG
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
$shrinkage = $max_width/$imagewidth;
$dest_height = $shrinkage * $imageheight;
$dest_width = $max_width;
$src_img = imagecreatefromjpeg("$input_file_path/$input_file_name");
if ($gdVersionTwo == False)
{
$dst_img = imageCreate($dest_width, $dest_height);
}
else
{
$dst_img = imageCreateTrueColor($dest_width, $dest_height);
}
//copy the original image info into the new image with new dimensions
//checking to see which function is available
if ($gdVersionTwo == False)
{
ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
}
else
{
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
}
$img_name = "$input_file_name";
imagejpeg($dst_img, "$input_file_path/$img_name", $quality);
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if $imagetype == 2
elseif ($imagetype == 3)
{
// it's a PNG
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
$shrinkage = $max_width/$imagewidth;
$dest_height = $shrinkage * $imageheight;
$dest_width = $imageWidth;
$src_img = imagecreatefrompng("$input_file_path/$input_file_name");
$dst_img = imagecreate($dest_width,$dest_height);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_width,$dest_height, $imagewidth, $imageheight);
$img_name = "$input_file_name";
imagepng($dst_img, "$input_file_path/$img_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if $imagetype == 3
} // end function resize_img_gd
/* --------------------------------------------------------------------------- */
function do_upload($file, $dest_dir) {
global $clearUploads;
$file['name'] = strtr($file['name'],"()!$'?: ,&+-/ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ","***** Manual signatures are NOT allowed *****_SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
$file['name'] = strtolower($file['name']);
$tempFileExt = substr($file['name'], -4);
$tempFileName = $file['name'];
$tempFileName = preg_replace('/' . $tempFileExt . '$/', '', $tempFileName);
$tempFileName = strtr($tempFileName, ".", "_");
$tempFileName = preg_replace('/_+/', '_', $tempFileName);
$tempFileName = preg_replace('/_+$/', '', $tempFileName);
$file['name'] = $tempFileName . $tempFileExt;
$dest_dir = ereg_replace("//", "/", $dest_dir);
$srcFile = $dest_dir . $file['name'];
//$srcFile = ereg_replace("//", "/", $srcFile);
if (file_exists($srcFile)) {
mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], "Upload FAILED. File allready exists" );
}
$format = substr( $file['name'], -3 );
$allowable = array (
'xcf',
'odg',
'gif',
'jpg',
'epg',
'png',
'bmp',
'doc',
'txt',
'xls',
'csv',
'ppt',
'swf',
'pdf',
'odt',
'ods',
'odp'
);
$noMatch = 0;
foreach( $allowable as $ext ) {
if ( strcasecmp( $format, $ext ) == 0 ) $noMatch = 1;
}
if(!$noMatch){
mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], 'Este tipo de archivo no es permitido: ' . $file['name'] );
}
$allowableImg = array (
'gif',
'jpg',
'png'
);
$matchImg = 0;
foreach( $allowableImg as $ext ) {
if ( strcasecmp( $format, $ext ) == 0 ) $matchImg = 1;
}
if (!move_uploaded_file($file['tmp_name'], $srcFile)){
mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], "Upload FAILED: " . $file['name'] );
}
else {
if($matchImg){
make_thumb_gd ($file['name'], $dest_dir);
resize_img_gd ($file['name'], $dest_dir);
}
mosChmod($srcFile);
mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], "Upload complete: " . $file['name'] );
}
$clearUploads = true;
}
/* --------------------------------------------------------------------------- */
Remember to create an .htaccess with the following php value:
php_value memory_limit 30M
This is because the way of GD works.
More info:
http://qdig.sourceforge.net/Support/AllowedMemorySize
Any help or comment is welcome.
I did a small hack in the back-end of media manager. This hack creates a thumbnail of the original image and also resize the image to a specific width. Im using GD2 not imageMagick, also is cofigurable to be used with GD1.
If you want to try this small hack, back-up and edit the file:
/administrator/components/com_media/admin.media.php
Replace the function:
function do_upload($file, $dest_dir)
and add at top of function do_upload two new functions:
/* --------------------------------------------------------------------------- */
// makes a thumbnail using the GD library
function make_thumb_gd ($input_file_name, $input_file_path, $jpegQuality = 70, $thumbnailWidth = 100, $gdVersionTwo = True )
{
$quality = $jpegQuality;
// first, grab the dimensions of the pic
$imagedata = GetImageSize($input_file_path.'/'.$input_file_name);
$imagewidth = $imagedata[0];
$imageheight = $imagedata[1];
$imagetype = $imagedata[2];
// type definitions
// 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP
// 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order)
// 9 = JPC, 10 = JP2, 11 = JPX
$tempFileExt = substr($input_file_name, -4);
$tempFileName = $input_file_name;
$tempFileName = preg_replace('/' . $tempFileExt . '$/', '', $tempFileName);
$tempFileName = strtr($tempFileName, ".", "_");
$tempFileName = preg_replace('/_+/', '_', $tempFileName);
$tempFileName = preg_replace('/_+$/', '', $tempFileName);
$thumb_name = $tempFileName . '_thumb_' . $tempFileExt;
// the GD library, which this uses, can only resize GIF, JPG and PNG
if ($imagetype == 1)
{
// it's a GIF
// figure out the ratio to which it should be shrunk, if at all
// see if GIF support is enabled
if (imagetypes() & IMG_GIF)
{
$shrinkage = 1;
if ($imagewidth > $thumbnailWidth)
{
$shrinkage = $thumbnailWidth/$imagewidth;
}
$dest_height = $shrinkage * $imageheight;
$dest_width = $thumbnailWidth;
$src_img = imagecreatefromgif("$input_file_path/$input_file_name");
$dst_img = imageCreate($dest_width, $dest_height);
//copy the original image info into the new image with new dimensions
//checking to see which function is available
ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagegif($dst_img, "$input_file_path/$thumb_name");
mosChmod("$input_file_path/$thumb_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if GIF support is enabled
} // end if $imagetype == 1
elseif ($imagetype == 2)
{
// it's a JPG
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
if ($imagewidth > $thumbnailWidth)
{
$shrinkage = $thumbnailWidth/$imagewidth;
}
$dest_height = $shrinkage * $imageheight;
$dest_width = $thumbnailWidth;
$src_img = imagecreatefromjpeg("$input_file_path/$input_file_name");
if ($gdVersionTwo == False)
{
$dst_img = imageCreate($dest_width, $dest_height);
}
else
{
$dst_img = imageCreateTrueColor($dest_width, $dest_height);
}
//copy the original image info into the new image with new dimensions
//checking to see which function is available
if ($gdVersionTwo == False)
{
ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
}
else
{
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
}
imagejpeg($dst_img, "$input_file_path/$thumb_name", $quality);
mosChmod("$input_file_path/$thumb_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if $imagetype == 2
elseif ($imagetype == 3)
{
// it's a PNG
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
if ($imagewidth > $thumbnailWidth)
{
$shrinkage = $thumbnailWidth/$imagewidth;
}
$dest_height = $shrinkage * $imageheight;
$dest_width = $thumbnailWidth;
$src_img = imagecreatefrompng("$input_file_path/$input_file_name");
$dst_img = imagecreate($dest_width,$dest_height);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_width,$dest_height, $imagewidth, $imageheight);
imagepng($dst_img, "$input_file_path/$thumb_name");
mosChmod("$input_file_path/$thumb_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if $imagetype == 3
return $thumb_name;
} // end function make_thumb_gd
// resizes image using the GD library
function resize_img_gd ($input_file_name, $input_file_path, $jpegQuality = 70, $maxWidth = 400, $imageWidth = 400, $gdVersionTwo = True)
{
$quality = $jpegQuality;
$max_width = $maxWidth;
// first, grab the dimensions of the pic
$imagedata = getimagesize($input_file_path.'/'.$input_file_name);
$imagewidth = $imagedata[0];
$imageheight = $imagedata[1];
$imagetype = $imagedata[2];
// type definitions
// 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP
// 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order)
// 9 = JPC, 10 = JP2, 11 = JPX
$img_name = $input_file_name; //by default
// the GD library, which this uses, can only resize GIF, JPG and PNG
if ($imagetype == 1)
{
// it's a GIF
// see if GIF support is enabled
if (imagetypes() & IMG_GIF)
{
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
$shrinkage = $max_width/$imagewidth;
$dest_height = $shrinkage * $imageheight;
$dest_width = $max_width;
$src_img = imagecreatefromgif("$input_file_path/$input_file_name");
$dst_img = imageCreate($dest_width, $dest_height);
//copy the original image info into the new image with new dimensions
//checking to see which function is available
ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagegif($dst_img, "$input_file_path/$img_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} //end if GIF support is enabled
} // end if $imagetype == 1
elseif ($imagetype == 2)
{
// it's a JPG
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
$shrinkage = $max_width/$imagewidth;
$dest_height = $shrinkage * $imageheight;
$dest_width = $max_width;
$src_img = imagecreatefromjpeg("$input_file_path/$input_file_name");
if ($gdVersionTwo == False)
{
$dst_img = imageCreate($dest_width, $dest_height);
}
else
{
$dst_img = imageCreateTrueColor($dest_width, $dest_height);
}
//copy the original image info into the new image with new dimensions
//checking to see which function is available
if ($gdVersionTwo == False)
{
ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
}
else
{
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
}
$img_name = "$input_file_name";
imagejpeg($dst_img, "$input_file_path/$img_name", $quality);
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if $imagetype == 2
elseif ($imagetype == 3)
{
// it's a PNG
// figure out the ratio to which it should be shrunk, if at all
$shrinkage = 1;
$shrinkage = $max_width/$imagewidth;
$dest_height = $shrinkage * $imageheight;
$dest_width = $imageWidth;
$src_img = imagecreatefrompng("$input_file_path/$input_file_name");
$dst_img = imagecreate($dest_width,$dest_height);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_width,$dest_height, $imagewidth, $imageheight);
$img_name = "$input_file_name";
imagepng($dst_img, "$input_file_path/$img_name");
imagedestroy($src_img);
imagedestroy($dst_img);
} // end if $imagetype == 3
} // end function resize_img_gd
/* --------------------------------------------------------------------------- */
function do_upload($file, $dest_dir) {
global $clearUploads;
$file['name'] = strtr($file['name'],"()!$'?: ,&+-/ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ","***** Manual signatures are NOT allowed *****_SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
$file['name'] = strtolower($file['name']);
$tempFileExt = substr($file['name'], -4);
$tempFileName = $file['name'];
$tempFileName = preg_replace('/' . $tempFileExt . '$/', '', $tempFileName);
$tempFileName = strtr($tempFileName, ".", "_");
$tempFileName = preg_replace('/_+/', '_', $tempFileName);
$tempFileName = preg_replace('/_+$/', '', $tempFileName);
$file['name'] = $tempFileName . $tempFileExt;
$dest_dir = ereg_replace("//", "/", $dest_dir);
$srcFile = $dest_dir . $file['name'];
//$srcFile = ereg_replace("//", "/", $srcFile);
if (file_exists($srcFile)) {
mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], "Upload FAILED. File allready exists" );
}
$format = substr( $file['name'], -3 );
$allowable = array (
'xcf',
'odg',
'gif',
'jpg',
'epg',
'png',
'bmp',
'doc',
'txt',
'xls',
'csv',
'ppt',
'swf',
'pdf',
'odt',
'ods',
'odp'
);
$noMatch = 0;
foreach( $allowable as $ext ) {
if ( strcasecmp( $format, $ext ) == 0 ) $noMatch = 1;
}
if(!$noMatch){
mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], 'Este tipo de archivo no es permitido: ' . $file['name'] );
}
$allowableImg = array (
'gif',
'jpg',
'png'
);
$matchImg = 0;
foreach( $allowableImg as $ext ) {
if ( strcasecmp( $format, $ext ) == 0 ) $matchImg = 1;
}
if (!move_uploaded_file($file['tmp_name'], $srcFile)){
mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], "Upload FAILED: " . $file['name'] );
}
else {
if($matchImg){
make_thumb_gd ($file['name'], $dest_dir);
resize_img_gd ($file['name'], $dest_dir);
}
mosChmod($srcFile);
mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], "Upload complete: " . $file['name'] );
}
$clearUploads = true;
}
/* --------------------------------------------------------------------------- */
Remember to create an .htaccess with the following php value:
php_value memory_limit 30M
This is because the way of GD works.
More info:
http://qdig.sourceforge.net/Support/AllowedMemorySize
Any help or comment is welcome.