Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Picture resize and resave from forum

    I have a method that grabs a image from a forum, saves it, puts it in the database, and it works fine. I just want to resize it without opening the file or anything.

    Which of these Would I resize?
    I thought it was the tmp_name, but that just makes a huge error.

    $fileName = $_FILES['Photo']['name'];
    $tmpName = $_FILES['Photo']['tmp_name'];
    $fileSize = $_FILES['Photo']['size'];
    $fileType = $_FILES['Photo']['type'];

    Any ideas would be great.

    Thanks

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    You can't resize the image this way. If you want the actual image to be resized you will need to use something like ImageMagick or the GD library. clicking the links will give you documentation on how to install these. If you are with a hosting company then you need to check whether they have any of these extensions installed for your use. If not you might be stuck with the images the size they are and just use html to display the images at different sizes with the width attribute. If you have got GD let us know!
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    New Coder
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    80
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Here's a function I made to resize images and keep the aspect ratio

    It uses constants that I defined in an include
    IMG_RESIZE_HEIGHT and IMG_RESIZE_WIDTH just replace those values.


    But this stores the images on your filesystem rather than the database but I think you could still make it work.
    Oh yeah, and it requires GD like timgolding mentioned.

    $source = path/to/original/image
    $destination = /path/to/be/saved


    PHP Code:
    function resize_image$source$destination$thumbnail false )
    {
        list( 
    $width$height$type ) = getimagesize$source );
        
        switch( 
    $type ) {
            case 
    IMAGETYPE_GIF:
                
    $image imagecreatefromgif$source );
                break;
            case 
    IMAGETYPE_JPEG:
                
    $image imagecreatefromjpeg$source );
                break;
            case 
    IMAGETYPE_PNG:
                
    $image imagecreatefrompng$source );
                break;
        }
        
        if( 
    $image === false )
            return 
    false;
        
        
    $aspect_ratio $width $height;
        if( 
    $thumbnail === true ) {
            if( 
    $width IMG_PREVIEW_WIDTH )
                
    $new_width IMG_PREVIEW_WIDTH;
            else
                
    $new_width $width;
            
            if( 
    $height IMG_PREVIEW_HEIGHT )
                
    $new_height IMG_PREVIEW_HEIGHT;
            else
                
    $new_height $height;
        } 
        else {
            if( 
    $width IMG_RESIZE_WIDTH )
                
    $new_width IMG_RESIZE_WIDTH;
            else
                
    $new_width $width;

            if( 
    $height IMG_RESIZE_HEIGHT )
                
    $new_height IMG_RESIZE_HEIGHT;
            else
                
    $new_height $height;
        }
        
        if( 
    $width $height )
            
    $new_height = (int) $new_width $aspect_ratio
        
        if( 
    $height $width )
            
    $new_width = (int) $new_height $aspect_ratio

        
    $new_image = @imagecreatetruecolor$new_width$new_height );
        
    imagecopyresampled$new_image$image0000$new_width$new_height$width$height );
        
        switch( 
    $type ) {
            case 
    IMAGETYPE_GIF:
                
    imagegif$new_image$destination );
                break;
            case 
    IMAGETYPE_JPEG:
                
    imagejpeg$new_image$destination100 );
                break;
            case 
    IMAGETYPE_PNG:
                
    imagepng$new_image$destination);
                break;
        }

    Last edited by Truffle; 11-18-2011 at 04:15 PM.

  • Users who have thanked Truffle for this post:

    dawg (11-18-2011)

  • #4
    New Coder
    Join Date
    Nov 2011
    Location
    New England
    Posts
    62
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Check out this link:

    http://www.les-stooges.org/pascal/we...ndex.php?la=en

    I use this to resize images on-the-fly. Download the script, place it in your main directory http directory. I forget the original name but I named mine img.php. Then in your website add

    Code:
    <img src='/img.php?f=path/to/image.jpg&amp;w=100' />
    Or you can specify &h=100 for 100px height. It also has shadows and frame options!

    One note, if you enable caching and try to change the shadow/frame/width/height, you might need to clear the temp directory for it to reflect changes.

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Awesome man, I spent 4 hours trying to get something that works, yours is beautiful.

    Thanks a million,
    Dawg


  •  

    Posting Permissions

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