Imagemagick code to GD

Discussion in 'PHP' started by overflo, May 5, 2008.

  1. #1
    Hi

    I'm in dire straits. Just spent ages putting together a dynamic site, uploaded and have found out that hosts have disabled exec functions due to security problems. I am so far very happy otherwise with hosts (just had a very bad experience with previous) the trouble is, as I also work and am primary carer for disabled son I don't have the time to learn the differences between Imagemagick and GD.
    If anyone could start me off in the right direction by converting the following code (that gets the image, finds shortest edge and resizes so shortest edge is 300 the crops to 300x300) I will be eternally grateful.

    Cheers
    
    # $img  = "pathtoimage/image.jpg";
    # $size = GetImageSize($img);
    # $dim  = "300x300+0+0";
    #  echo $size[0] . " " . $size[1];
    #     // Wide Image
    #     if($size[0] > $size[1]) {  
    #         $thumbnail_height = 300;  
    #         $thumbnail_width = (int)(300 * $size[0] / $size[1]);  
    #     }  
    #    
    #     // Tall Image
    #     else{
    #          $thumbnail_height = (int)(300 * $size[1] / $size[0]);
    #          $thumbnail_width = 300;
    #     }
    #        
    # $mogrify = "pathtimagemagick/mogrify -geometry";
    # $crop    = "pathtimagemagick/mogrify -gravity Center -crop";
    # system("$mogrify $thumbnail_heightx$thumbnail_width $img", $exec_retval);
    # system("$crop $dim $img", $exec_retval);
    
    Code (markup):
     
    overflo, May 5, 2008 IP
  2. graham23s

    graham23s Well-Known Member

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #2
    heres a function i wrote for my sites:

    
    function resize_image($upload_directory, $new_image_name)
    {
      // original image location //
      $original_image = $upload_directory;
      
      // set up a canvas sizes //
      $canvas_width = 65;
      $canvas_height = 65;
      
      // create the canvas //
      $canvas = imagecreatetruecolor($canvas_width, $canvas_height);
      
      // make the background color white //
      $white_background = imagecolorallocate($canvas, 255, 255, 255);
      
      // change the background to white //
      imagefill($canvas, 0, 0, $white_background);
      
      // get the image height and width //
      list($image_width, $image_height) = getimagesize($upload_directory);
      
      #########################################
      // RATIO CALCULATIONS //
      $ratio = $image_width / $image_height;
      
        if ($ratio > 1 )
        {
      
      	$new_image_width = 65;
    		$new_image_height = 65 / $ratio;
      
         } else {
       
       	$new_image_width = (float) 65 * $ratio;
    		$new_image_height = 65;
       
      }
      // RATIO CALCULATIONS //
      #########################################
      
      // store original into memory //
      $original_image = imagecreatefromjpeg($original_image);
      
      // copy the original image onto the canvas canvas, original and top/left co-ordinates //
      imagecopyresampled($canvas, $original_image, 0,0,0,0, $new_image_width, $new_image_height, $image_width, $image_height);
      
      // thumbnail name //
      $new_thumbnail_name = "thumb-$new_image_name";
      
      // save the thumbnail in the thumbs folder //
      if(imagejpeg($canvas, "products/thumbnails/$new_thumbnail_name", 100))
      {
       return("$new_thumbnail_name");
      }
      
      // destroy the images in memory //
      imagedestroy($original_image);
      imagedestroy($canvas);  
      
    } // end function //
    
    PHP:
    just change the sizes to 300 :) if you want any help ill incorporate it for you.

    Graham

    EDIT: it only works for .jpgs but it can be easily modified for others.
     
    graham23s, May 5, 2008 IP
  3. overflo

    overflo Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, your a champion. I've got it half working. It creates a blank .jpg but I get the following error:

    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in

    looks like I may have to change servers after all::(
     
    overflo, May 5, 2008 IP
  4. graham23s

    graham23s Well-Known Member

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #4
    Hi,

    Can you post your code, theres a few extra bits and bobs in there you probably wouldnt need:)

    Graham
     
    graham23s, May 5, 2008 IP
  5. overflo

    overflo Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    HI Graham.

    back to using your code, it's nearly working. I've attached images. An original, the desired outcome and the image that the code is generating. Could you have a look an see if you can determine what I'm not getting right,

    Cheers
     

    Attached Files:

    overflo, May 7, 2008 IP