image sizing/cropping

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Before I get too involved, how difficult is it going to be to create a
page where users can upload an image then resize or crop it to a
specific size?  I have a simple directory, and would like to allow
listers be able to put images in their listing, but the images need to
be limited in height and width without me having to process each one.

Re: image sizing/cropping

<William Gill>
<Wed, 31 Dec 2008 01:04:38 -0500>

Quoted text here. Click to load it

Its quite easy if you look at it as a logical step by step process .

But - although many users on here have been helped with the type of
question you have just asked in the past - they will ignore your post as
they arnt interested in passing on the knowledge .

Are you the type of user who if they get told how to do it - will ignore
any similar type of resize/crop questions asked in the future ? .


Re: image sizing/cropping

Krustov wrote:
Quoted text here. Click to load it

And Krustov, here I was so interested in hearing YOUR solutions.

OP: I will give you a solution I used when I get back from work, unless
someone beats me to the punch.


Re: image sizing/cropping

the following:

Quoted text here. Click to load it

It's not difficult provided you have access to the necessary functions.
The only problem I have come across is that the large size of many pics
from modern cameras can cause problems.

Geoff Berrow  0110001001101100010000000110
100110001101101111001011100111010101101011 -

Re: image sizing/cropping

William Gill wrote:
Quoted text here. Click to load it
  have a look there
it solve lot of my problem that i had with gd on big photo

Re: image sizing/cropping

Daniele wrote:
Quoted text here. Click to load it

I scanned, but none of the imagemagick extensions appear
to be installed on the server so I don't see how this helps me.

Re: image sizing/cropping

William Gill wrote:
Quoted text here. Click to load it

You can do it without imagemagick, but it's a bit slower.  All you need
are the GD functions.

Basically, you create a new image and copy the old one.  Some sample
code adapted from another project (untested, so no guarantees, but it's

// function resizeImage
// Resizes an image, maintaining the aspect ratio
// Input: full path and filename to image to be resized,
//        new width
//        new height
// Output: handle to a jpg image, or NULL if an error

function resizeImage($image, $x, $y)
   if (file_exists($image)) {
     if ($x > 0 && $y > 0) {
       $image_size =  getimagesize($image);
       if ($image_size[0] > 0 && $image_size[1] > 0) {
         $x_ratio = $x / $image_size[0];
         $y_ratio = $y / $image_size[1];
         if ($x_ratio < $y_ratio) {
           $y = intval(round($image_size[1] * $x_ratio));
         } else {
           $x = intval(round($image_size[0] * $y_ratio));
       } else {
         return null;
       $image_show = imagecreatetruecolor($x, $y);
       switch ($image_size['mime']) {
         case 'image/jpeg':
           $image_in = imagecreatefromjpeg($image);
         case 'image/gif':
           $image_in = imagecreatefromgif($image);
         case 'image/png':
           $image_in = imagecreatefrompng($image);
               return null;
       imagecopyresampled($image_show, $image_in, 0, 0, 0, 0, $x, $y,
$image_size[0], $image_size[1]);
       return $image_show;
   return null;

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: image sizing/cropping


    Uploaded image file is authenticated as mime type image, temporary
    directory is created, optional thumbnail is generated,
    contents of directory (uploaded image and thumbnail (opt) ) are
moved to
    desired directory location.

class upload_image

 const TEMP_DIR = "temp_imgs"; // Temporary image folder

public function save_image($form_fieldname = 'file',
                           $image_destination = './',
                           $create_thumb = FALSE,
                           $thumbnail_size = 150){


       $img_mime_type = getimagesize($_FILES[$form_fieldname]

       if(($img_mime_type[2] == IMAGETYPE_GIF) ||
         ($img_mime_type[2] == IMAGETYPE_JPEG) ||
         ($img_mime_type[2] == IMAGETYPE_PNG))

           //create temp images directory



           //move the uploaded file into the temp directory

           $picture_name = $_FILES[$form_fieldname]['name'];

           if ($create_thumb){

              //create thumbnail of uploaded img

              //move the thumbnail
              } else {
                      echo "<br /><b>! An Image Thumb with that name
already exists</b><br />";


           //move the full sized image
           } else {
                   echo "<b>! An image with that name already exists</

           //remove the temp image directory   // (needs some "delete
all files" functionality really, just incase)

       } else {
               die('You may only upload file types JPEG/JPG, PNG or


} // End function upload_image

//  function createthumb creates a thunbnail of an image
//  @param

public function createthumb($name,$filename,$new_w,$new_h){
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    if (preg_match('/jpe?g/i', $ext)) {
        $src_img = imagecreatefromjpeg($name);
    if (preg_match('/png/i', $ext)) {
        $src_img = imagecreatefrompng($name);

           $old_x = imageSX($src_img);
           $old_y = imageSY($src_img);

              if ($old_x >= $old_y) {
                 $thumb_w = $new_w;
                 $thumb_h = $old_y * ($new_h/$old_x);
              if ($old_x < $old_y) {
              $thumb_w = $old_x * ($new_w/$old_y);

   $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);


   if (preg_match('/png/i', $ext)) {
   } else {




} // End Class



$upload = new upload_image();




from my library at : =

Re: image sizing/cropping

Thanks all.  I have to digest everything I've gotten so far.  Ultimately
I want to do more than just resize (actually I don't want to do anything
I need the user to be able to do it them self <G>).  I want, for
example, to allow them (something like)a 150px by 300px image "space"
and allow them to fit any part of their their 600px by 300px image into
the resulting image.

If I get it figured out I'll post the results here so Krustov can have
some of his faith in humanity restored <g>.

Site Timeline