SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,426
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)

    Resizing an image

    I have a php script that allows a user to submit a story with pictures to one of my websites. The pictures are uploaded through a form, renamed, then moved to their proper folder. One thing I want to do before they are moved is verify the file isn't very large in terms of dimensions (not file size). If it is, I want to resize it to a more manageable size.

    I have the basics of what I'm trying to down (or so I think ) :

    Code:
    // Set max width/height
    $max = 400;
    
    // Create an instance of the image from the file upload
    $image = ImageCreateFromJPEG($_FILES['userfile']['tmp_name'][$i]);
    
    // Get its dimensions
    $image_width = ImageSX($image);
    $image_height = ImageSY($image);
    
    // Determine is width/height proportions
    if (($image_height > $max) || ($image_width > $max)) {
        if ($image_height > $image_width) {
            $sizefactor = (double) ($max / $image_height);
        }
        else {
            $sizefactor = (double) ($max / $image_width) ;
        }
    }
    
    // Determine the new width and height
    $newwidth = (int) ($image_width * $sizefactor);
    $newheight = (int) ($image_height * $sizefactor);
    
    // Create the smaller canvas
    $newimage = imagecreate ($newwidth, $newheight);
    
    // Resize the image
    imagecopyresampled($newimage,$image, 0, 0, 0, 0, $newwidth, $newheight, $image_width, $image_height);
    
    // Make the new file a jpg ...I think...
    imagejpeg($newimage);
    
    // What do I do with it?
    What I'm not sure about is what exactly do I have when I'm done? I'm assuming an image but how do I refer to it so I can move it to the proper folder?

    Or, did I make a mistake somewhere?

  2. #2
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to output $newimage.

  3. #3
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, use that one instead:

    echo imagejpeg($newimage);

  4. #4
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,426
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    My goal is not to output it to a browser but to save it instead. Will this do that? Or will it just output it to the browser?

  5. #5
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,426
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Ok, I missed a parameter in imagejpeg and that solved my naming problem. But now I get this error:

    Warning: imagejpeg(): Unable to open '108_0.jpeg' for writing on line 75 (108_0.jpeg is the name I gave the image in the variable $newname[$i])

    Code:
    // Set max width/height
    $max = 450;
    
    // Create an instance of the image from the file upload
    $image = ImageCreateFromJPEG($_FILES['userfile']['tmp_name'][$i]);
    
    // Get its dimensions
    $image_width = ImageSX($image);
    $image_height = ImageSY($image);
    
    // Determine is width/height proportions
    if (($image_height > $max) || ($image_width > $max)) {
        if ($image_height > $image_width) {
            $sizefactor = (double) ($max / $image_height);
        }
        else {
            $sizefactor = (double) ($max / $image_width) ;
        }
    }
    
    // Determine the new width and height
    $newwidth = (int) ($image_width * $sizefactor);
    $newheight = (int) ($image_height * $sizefactor);
    
    // Create the smaller canvas
    $newimage = imagecreate ($newwidth, $newheight);
    
    // Resize the image
    imagecopyresampled($newimage,$image, 0, 0, 0, 0, $newwidth, $newheight, $image_width, $image_height);
    
    // Make the new file a jpg ...I think...
    imagejpeg($newimage, $newname[$i]);

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, does the image exist in the same folder as the script does and does the script have enough 'rights' to write?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •