SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Location
    Leeds, United Kingdom
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb How to resize and display database URL images using PHP?

    Hi there,

    I'm trying to display and re-size images using URL's which are held in a Mysql database (under the column named "ImageUrl"),
    the images are all from separate sources and so I need to re size them appropriately before displaying them in separate Divs, I'm still trying to get my head around a lot of PHP, so just to let you know that I'm no expert.

    So far I have:
    -Connected to the database
    -Ran the query
    -Then displayed the query result (So I can display the original image just fine by sourcing the link from the ImageUrl column, but its still massive)

    So next, i suspect that I need to use the GB library to store the image after resizing it, before displaying it.
    I have found find plenty of example's online illustrating how to do this using an image which is already saved on the server, but none on how to do it using a URL sourced from a database.

    And just in-case the question still seems hazy, my question is:
    How do I use PHP to call upon and re-size a database URL Image?

    Any replies will be greatly appreciated =D

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Well you can do it that way... as long as you dont ever link to an image bigger than PHP's memory limit.

    PHP will have to load the entire image to be able to resize it.

    file_get_contents the url (if your webhost allows remote file_get_contents), imagecreatefromstring the results, imagecreate an image in the desired size, and then imagecopyresized A into B. I would highly suggest then locally saving the result for quicker handling. (Note: Please make sure you have permission to copy said image from the remote site; we are not responsible for your copyright infringements!)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    You can do it with Imagemagick

    Page on its own called something like resize.php:
    PHP Code:
    $image $_GET["I"];
    exec("convert $image -resize 500x500 JPG:-"); 
    Where you want the image:
    PHP Code:
    $I result from the database query should be a URL;
    echo 
    "<img src=\"resize.php?I=$I\">"

  4. #4
    SitePoint Member
    Join Date
    Jan 2013
    Location
    Leeds, United Kingdom
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for both of your replies!
    I attempted Lions approach but I don't think Im experienced enough with those operators, well, not enough to put them all together anyway, although I did try

    Rubbles approach seemed easier, but I still cant seem to get it to work >_<
    1.I created a new file in my folder called resize.php
    2.I entered this code:
    <?php
    $image = $_GET["I"];
    exec("convert $image -resize 500x500 JPG:-");
    ?>
    And then saved it.

    3. In my events.php file, in the specific Div i have entered this code:
    <?php
    $I = $attresult['ImageUrl'];
    echo "<img src=\"resize.php?I=$I\">";
    ?>

    but I'm getting no image at all, or any error codes.

    Just to help keep it clear, this is my code for calling the URL from the ImageUrl Column:
    <?php
    $query1 = "SELECT * FROM attraction WHERE AttractionID=1";
    $attquery = mysql_query($query1) or die('query error has occured ' . mysql_error());
    $attresult = mysql_fetch_assoc($attquery);
    ?>

    I also use this code to pull the Name and Description Column's info.

    Thankyou!!!

    **Edited "Imageresizer" for "resize.php"**

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    What do you get if you go to resize.php?l=<a valid thing>? I'm gonna go ahead and guess you're being denied access to exec, as thats a very dangerous function to give people access to on a shared host.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Member
    Join Date
    Jan 2013
    Location
    Leeds, United Kingdom
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um..

    When going to this address (The actual link is the target link from my database):
    http://localhost/database/resize.php...2BExchange.jpg

    it says:
    Notice: Undefined variable: attresult in C:\xampp\htdocs\database\resize.php on line 2

    But I think my PHP is correct?

  7. #7
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    It is terrible as I can not remember how this works now as I only did it once

    I think the $_GET is incorrect as you do not have a URL to "get" from. I will need to think about it for a while.

  8. #8
    SitePoint Member
    Join Date
    Jan 2013
    Location
    Leeds, United Kingdom
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem! Thanks again for your help, I'l keep checking back!

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    What is the content of resize.php ?

    Copy, paste, and put ["PHP"] ["/PHP"] around it (take the quotes out). Cause what you showed us doesnt call for $attresult.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  10. #10
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,406
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I have been trying to get this to work on my localhost and it would not - uploaded to my server and it does. There must be something in the setup. I see you are using a localhost as well; if it does not work I would try it on the server.

    I also started with an image in the same folder as the code to get rid of any other problems; once this works add the database part etc. one bit at a time.

    resize.php:
    PHP Code:
    <?php

    $Im 
    = isset($_GET['I']) ? $_GET['I'] : 'default.png';

    $cmd "/usr/local/bin/convert $Im -thumbnail 200x200 "
    " -unsharp 0.2x0.6+1.0 -quality 100 JPG:-"

    header("Content-type: image/jpeg"); 
    passthru($cmd$retval);  

    ?>
    main test page:
    PHP Code:
    <?php
     $image 
    'th__MG_6463.jpg';
     echo 
    "<img src=\"resize.php?I=$image\">"
    ?>
    I assume you have Imagemagick installed and you may need to change convert to something like /usr/local/bin/convert depending on how your server is setup.


Tags for this Thread

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
  •