Thumbnail and Resize Image with Media Manager :: Joomla 1.0.4

Your code modifications and patches you want to share with others.
User avatar
salchi
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Dec 20, 2005 1:04 am

Thumbnail and Resize Image with Media Manager :: Joomla 1.0.4

Postby salchi » 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 *****");
      $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.

User avatar
guilliam
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4180
Joined: Thu Aug 18, 2005 10:27 am
Location: Sunny City Cebu, Philippines!
Contact:

Re: Thumbnail and Resize Image with Media Manager :: Joomla 1.0.4

Postby guilliam » Tue Dec 20, 2005 11:24 am

most helpful. thanks for sharing this one.

[ MODERATOR NOTE: moving to the appropriate forum ]

- g
"I was one of those who wondered why people would pay so much $$$$ to do something that was so much fun!" -R. Harkrider, Fortran Code Engr.

http://www.joomlaconsultancy.net

User avatar
brian
Joomla! Master
Joomla! Master
Posts: 11295
Joined: Fri Aug 12, 2005 7:19 am
Location: Leeds, UK
Contact:

Re: Thumbnail and Resize Image with Media Manager :: Joomla 1.0.4

Postby brian » Tue Dec 20, 2005 11:32 am

Have a look at the media manager replacement component from http://www.scotweb.com as I believe it already does this

Brian
"Exploited yesterday... Hacked tomorrow"
Blog http://brian.teeman.net/
Joomla Hidden Secrets http://hiddenjoomlasecrets.com/

User avatar
salchi
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Dec 20, 2005 1:04 am

Re: Thumbnail and Resize Image with Media Manager :: Joomla 1.0.4

Postby salchi » Tue Dec 20, 2005 1:55 pm

Thanx Guilliam, Brian.

I did the same hack to tmedit:
http://www.xhtmlsuite.com/downloads/tas ... ew/gid,83/


brian wrote:Have a look at the media manager replacement component from http://www.scotweb.com as I believe it already does this



Cool, im going to try it:
http://www.scotweb.com/component/option ... Itemid,64/
Last edited by salchi on Tue Dec 20, 2005 2:50 pm, edited 1 time in total.

Ansl72
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Sat Feb 14, 2009 10:44 am

Re: Thumbnail and Resize Image with Media Manager :: Joomla 1.0.

Postby Ansl72 » Thu Mar 05, 2009 11:53 am

Hi Salchi excuse me for my english I read the your last post in this forum around
the artcle "Thumbnail and Resize Image with Media Manager :: Joomla 1.0.4"
I would know if I can use this hack for joomla1.5.9, but I can't do it
becouse I'm beginner in PHP language!

please you can help me?
thanks in advance

Angelo


gdanx
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Tue Jan 17, 2006 11:30 pm

Re: Thumbnail and Resize Image with Media Manager :: Joomla 1.0.

Postby gdanx » Tue Jun 02, 2009 6:33 pm

Hi,

I'm willing to pay someone to port this script for Joomla 1.5. I don't need thumbnail creations, all I need is to resize the uploaded image to a specific width.

PM me if you're interested.

Thanx!

User avatar
ooffick
Joomla! Master
Joomla! Master
Posts: 11063
Joined: Thu Jul 17, 2008 3:10 pm
Location: Ireland
Contact:

Re: Thumbnail and Resize Image with Media Manager :: Joomla 1.0.

Postby ooffick » Wed Jun 03, 2009 11:11 am

Did you check this Extension:
http://extensions.joomla.org/extensions/5694/details

Olaf
Olaf Offick - Global Moderator
http://learnskills.org

gdanx
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Tue Jan 17, 2006 11:30 pm

Re: Thumbnail and Resize Image with Media Manager :: Joomla 1.0.

Postby gdanx » Wed Jun 03, 2009 2:10 pm

Hi,

yes, I've checked that extension, but I need something completely automatic (without the need of dragging it manually).

Dan

User avatar
ooffick
Joomla! Master
Joomla! Master
Posts: 11063
Joined: Thu Jul 17, 2008 3:10 pm
Location: Ireland
Contact:

Re: Thumbnail and Resize Image with Media Manager :: Joomla 1.0.

Postby ooffick » Wed Jun 03, 2009 2:32 pm

Hi Dan,

as far as I can see you don't need to do that, as you can just scale it down, but if you want to add a default value, you can just change the code of the plugin:
find the following code:

Code: Select all

      $width = $imgData[0];      


and replace it with this one:

Code: Select all

$width = 200;

to force it the image to a size of 200px, unless you specified a different value.

Olaf
Olaf Offick - Global Moderator
http://learnskills.org


Return to “Core Hacks and Patches”

Who is online

Users browsing this forum: No registered users and 0 guests