Need to convert images to jpg to make thumbnails

Discussion in 'PHP' started by Jikdor, Jul 18, 2007.

  1. #1
    When a image is uploaded it creates a thumbnail image, right now it only workes with jpeg, couldent finda funktion that converts other file types like bmp, tif, gif, png to a jpg? How can it be done?

    here is the script.

    
          function resize($imgH, $sizeX, $sizeY) {
          $imgR = imagecreatetruecolor($sizeX, $sizeY);
          imageCopyResized($imgR, $imgH, 0, 0, 0, 0, imageSX($imgR), imageSY($imgR), imageSX($imgH), imageSY($imgH));
     
          return $imgR;
       }
    
       
    $filename_thumb = $new_file_name;
    $output = 'thumbnails/'.$filename_thumb;
     
    $width_orginal = $width; //Width of the orginal image
    $height_orginal = $height; // Height of the orginal image
    
    if($width_orginal > $height_orginal) 
    {
    $biggest_side = $width_orginal;
    $smallest_side = $height_orginal;
    }
    else 
    {
    $biggest_side = $height_orginal;
    $smallest_side = $width_orginal;
    }
    
    $aspect_ratio = ($biggest_side)/($smallest_side);
    
    
    $short_side = (floor(150/($aspect_ratio)));
    if ($short_side == 0) $short_side = 1;
    
    if($width_orginal > $height_orginal) 
    {
    $new_width = 150;
    $new_height = $short_side;
    }
    else 
    {
    $new_height = 150;
    $new_width = $short_side;
    }
    
    
    
     
    
          $imgH = imageCreateFromJpeg('uploads/'.$filename_thumb);
          imageJpeg(resize($imgH, $new_width, $new_height), $output, 100);
    
    PHP:

     
    Jikdor, Jul 18, 2007 IP
  2. hans

    hans Well-Known Member

    Messages:
    2,923
    Likes Received:
    126
    Best Answers:
    1
    Trophy Points:
    173
    #2
    my gallery2 software uses
    ImageMagick
    to do such jobs. I also use ImageMagick offline on my linux machine to do such jobs very efficiently.

    From man ImageMagick:
    ImageMagick suite of tools. Use it to convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more.

    normally on most hosts ImageMagick might be preinstalled or it can be installed by you into your user-space.

    it's an ideal tool on Linux machines to batch process masses of pics in various formats efficiently.

    for more detals about PHP code - you may have a look at the code-modules of gallery2 - it's all open source
     
    hans, Jul 18, 2007 IP
  3. Jikdor

    Jikdor Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Seems way more complicated then it needs to be for what im trying to do? :)
     
    Jikdor, Jul 18, 2007 IP
  4. Corvus

    Corvus Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    To jpg images you are using the functions: imageCreateFromJpeg() and imageJpeg()

    To use other formats, try to detect file extension or myme type of the file and then use functions like:
    imagecreatefromgif()
    imagecreatefrompng()

    info+documentation: php.net
    etc...
     
    Corvus, Jul 18, 2007 IP
  5. Jikdor

    Jikdor Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Still havent figured out how to convert a iamge from png or gif to jpg...
     
    Jikdor, Jul 19, 2007 IP