Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2007
    Thanked 0 Times in 0 Posts

    Resize Uploaded Image

    I'm new to PHP, can anybody enlighten me on how I can get PHP to upload any image file, store the image path in a database for later use, resize the image to the following dimensions (keeping aspect ratio), then overwrite the original with the resized image.

    Resize Dimensions:

    WIDTH: 4.5m
    HEIGHT: 6m

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Thanked 637 Times in 625 Posts
    That's a lot to swallow, but start with imagecopyresampled() which also requires you know how to create images using functions such as imagecreatefromjpeg().

    Uploading files is a whole other animal... start here with this page in the manual.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Cottage Grove, Minnesota
    Thanked 1,096 Times in 1,087 Posts
    Using PHP GD libraries ... see Fumigator's links.

    It takes a bit of practice and experimentation to get it to work.
    There are some good examples though.

    For the "program flow", I suggest doing it this way ...

    1) Use a typical PHP script to upload the image into a "temp" directory.
    2) After the upload, redirect to another PHP script that takes the
    image, resizes it and saves it in the final directory and deletes the
    temporary image.
    3) It then writes the final path into the database.

    The reason I like using the intermediate temp directory is to avoid
    corrupting the final directory in the event something fails in the script.

    You can search Google for "PHP file upload script" to get that part,
    then use the GD examples and tutorials to get the resize part.
    The part where it saves into the final directory is done with GD, and
    the deleting from the temporary directory is done using "unlink".


    Posting Permissions

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