The Joomla! Forum ™





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
PostPosted: Tue Dec 20, 2005 2:08 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Dec 20, 2005 1:04 am
Posts: 6
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.


Top
 Profile  
 
PostPosted: Tue Dec 20, 2005 11:24 am 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso

Joined: Thu Aug 18, 2005 10:27 am
Posts: 4180
Location: Sunny City Cebu, Philippines!
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


Top
 Profile  
 
PostPosted: Tue Dec 20, 2005 11:32 am 
User avatar
Joomla! Master
Joomla! Master

Joined: Fri Aug 12, 2005 7:19 am
Posts: 10482
Location: Leeds, UK
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/


Top
 Profile  
 
PostPosted: Tue Dec 20, 2005 1:55 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Dec 20, 2005 1:04 am
Posts: 6
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.

Top
 Profile  
 
PostPosted: Thu Mar 05, 2009 11:53 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Sat Feb 14, 2009 10:44 am
Posts: 6
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


Top
 Profile  
 
PostPosted: Fri Mar 06, 2009 6:43 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Dec 20, 2005 1:04 am
Posts: 6
Hi!

Im so lazy, i didn't port this script to 1.5, because now im teaching my clients to use google picasa or tools like: http://quickthumbnail.com/

You can try:
http://extensions.joomla.org/extensions ... 13/details
http://joomlacode.org/gf/project/imageresize/frs/
http://extensions.joomla.org/extensions ... 63/details
http://extensions.joomla.org/extensions/5738/details
http://extensions.joomla.org/extensions ... 47/details


Top
 Profile  
 
PostPosted: Tue Jun 02, 2009 6:33 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Jan 17, 2006 11:30 pm
Posts: 28
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!


Top
 Profile  
 
PostPosted: Wed Jun 03, 2009 11:11 am 
User avatar
Joomla! Master
Joomla! Master

Joined: Thu Jul 17, 2008 3:10 pm
Posts: 10925
Location: Ireland
Did you check this Extension:
http://extensions.joomla.org/extensions/5694/details

Olaf

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


Top
 Profile  
 
PostPosted: Wed Jun 03, 2009 2:10 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Jan 17, 2006 11:30 pm
Posts: 28
Hi,

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

Dan


Top
 Profile  
 
PostPosted: Wed Jun 03, 2009 2:32 pm 
User avatar
Joomla! Master
Joomla! Master

Joined: Thu Jul 17, 2008 3:10 pm
Posts: 10925
Location: Ireland
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:
      $width = $imgData[0];      


and replace it with this one:
Code:
$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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 



Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group