Resize an image

Discussion in 'PHP' started by temp2, Oct 30, 2007.

Thread Status:
Not open for further replies.
  1. #1
    Can everybody show me method/extension which can be used to resize an image?
     
    temp2, Oct 30, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Oct 30, 2007 IP
  3. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #3
    @nico_swd: aha, if we use imagecopyresized, we must read image, but my site have a upload module for image, I want to resize those images as soon as visitors upload.
     
    temp2, Oct 30, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    No problem, just read the image like this:
    
    $image = imagecreatefromjpeg($_FILES['tmp_name']);
    
    PHP:
    And save it like this:
    
    imagejpeg($image, 'path/to/new/location/image.jpg');
    
    PHP:

    If you need help, you will have to post your code.
     
    nico_swd, Oct 30, 2007 IP
  5. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #5
    it's still read and create image from tmp file, I just want to resize tmp image directly
     
    temp2, Oct 30, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    You can't create it more directly. The image becomes available after it has been copied to the temp directory. You can't access it anywhere before that.
     
    nico_swd, Oct 30, 2007 IP
  7. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #7
    it's true, I just said that, after it had been copied to tmp directory, I want to resize that tmp image to new location.

    Your method:
    1. Create an image (in memory) from tmp image.
    2. Using imagecopyresized to resize image which has been create
     
    temp2, Oct 30, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    That's what my method does. imagejpeg() will save the newly created image in the wanted directory. And the temp file will be deleted after the script finishes its execution.

    You HAVE to read it in the memory, because you need it in an external library (GD or Imagick) to resize it.
     
    nico_swd, Oct 30, 2007 IP
  9. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #9
    I'll got a few really useful functions which I use all the time on my projects. They will resize images to a certain maximum height (or) width while keeping the ratio. Can also set the quality etc. Also have a function to create square thumbnails.

    What's your email address and I'll send you them?
     
    gordi555, Oct 30, 2007 IP
  10. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #10
    thank before gordi555, my email address is
     
    temp2, Oct 30, 2007 IP
  11. Rahulrulez

    Rahulrulez Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    use photoshop.. open image then go to Image --> Image size, then resize it and save it as any extension you want
     
    Rahulrulez, Oct 30, 2007 IP
  12. liam1412

    liam1412 Active Member

    Messages:
    387
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #12
    What kind of answer is that. He needs it doing as part of a script not just once. Im sure he knows how to resize an image !!!!!!! :rolleyes::confused:
     
    liam1412, Oct 30, 2007 IP
  13. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #13
    gordi555's scripts will convert the images in the memory too...
     
    nico_swd, Oct 31, 2007 IP
  14. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #14
    agreed...
    you dont need to save it ... imagejpeg() will just output the content to the browser
     
    bobb1589, Oct 31, 2007 IP
  15. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #15
    imagejpeg() will save it if the second parameter is set and the path exists.
     
    nico_swd, Oct 31, 2007 IP
  16. johnvixen

    johnvixen Active Member

    Messages:
    256
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #16
    Try Imagemagick as well
     
    johnvixen, Oct 31, 2007 IP
  17. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #17
    Sending email now...
     
    gordi555, Oct 31, 2007 IP
  18. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #18
    You have ONLY three options:

    * Use imagecopyresized to resize the image (either after upload or during).
    * Use on-fly resize which will resize the image but won't actually store it (just bandwidth waste), search google for such functions.
    * Javascript/PHP resize using width/height attributed.

    Peace,
     
    Barti1987, Oct 31, 2007 IP
Thread Status:
Not open for further replies.