SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    image manipulation

    i have images that are 90% A4 size and some that are post card size. when i click on the thumbnails i create i display the full sized image but A4 is massive to display so i need to shrink it down to fit on the screen.

    i create the thumbnails from the original image in the first place so i thought i could use the same code to just shrink the oringal image to a size thats suitable. (the code just grabs the image from the folder and just shrinks it down)

    to display the thumbnail is use this code :

    PHP Code:
    while ($row $result->fetch_assoc()) {
        
    echo 
    "<a href=\"image.php?id=" $row['id'] . "\">";
    echo 
    "<img src=\"imageResize.php?imageFilename=" $row['name'] . "&maxHeight=120,&maxWidth=150\" />\n";
    echo 
    "</a>\n";

    }
        
    ?> 
    but when i click on the thumbnail to display the full sized image i used this:

    PHP Code:
    while($row $result->fetch_row()) {
    echo 
    "<img src=\"" $row[2] . "\" />";

    i need to somehow in the src use imageResize but ive tried a fair amount of ways but have not figured it out yet. could someone please advise

    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    imageResize.php is a script that takes parameters and outputs an image. You need to use that as your src.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I have understood what you want:
    - Find the width and height of the image (original).
    PHP Code:
    list($width$height) = getimagesize($row['name']); 
    - Check if the image is bigger than the post card (or your default size) and use imageResize.php and pass the width and height if it is bigger one.
    PHP Code:
    if($width $postcardwidth || $height $postcardheight){
        echo 
    "<img src=\"imageResize.php?imageFilename=" $row[2] . "&maxHeight=" $height "&maxWidth=" $width "\" />\n"
    }
    else{
        echo 
    "<img src=\"" $row[2] . "\" />"

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i know i need to use it as my source as i have used it in my thumbnail creation but dont understand the code to use.

    what i think i need is how to use the imageResize file as my img src like the one for my thumbnails. but when i try doing something similair it doesnt display the image.

    could you please explain where i might be going wrong?

    also do you want to see the script i have got?

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by saunders1989 View Post
    also do you want to see the script i have got?
    yes

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is imageResize:

    PHP Code:
    <?php

    error_reporting
    (E_ALL &~ E_NOTICE);

    $image "C:/wamp/www/Blean_Photos/images/" $_GET['imageFilename'];

    switch(
    strtolower(substr($_GET['imageFilename'], -3))) {
    case 
    "jpg" :
    $fileType "jpeg";
    $imageCreateFunction "imagecreatefromjpeg";
    $imageOutputFunction "imagejpeg";
    break;
    case 
    "jpeg" :
    $fileType "jpeg";
    $imageCreateFunction "imagecreatefromjpeg";
    $imageOutputFunction "imagejpeg";
    break;
    case 
    "png" :
    $fileType "png";
    $imageCreateFunction "imagecreatefrompng";
    $imageOutputFunction "imagepng";
    break;
    }

    if(!
    $_GET['maxWidth']) {
    $maxWidth 100;
    } else {
    $maxWidth $_GET['maxWidth'];
    }

    if(!
    $_GET['maxHeight']) {
    $maxHeight 150;
    } else {
    $maxHeight $_GET['maxHeight'];
    }

    $size GetImageSize($image);
    $originalWidth $size[0];
    $originalHeight $size[1];

    $x_ratio $maxWidth $originalWidth;
    $y_ratio $maxHeight $originalHeight;

    // check that the new width and height aren't bigger than the original values.

    if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { // the new values are higher than the original, don't resize or we'll lose quality
    $newWidth $originalWidth;
    $newHeight $originalHeight;
    } else if ((
    $x_ratio $originalHeight) < $maxHeight) {
    $newHeight ceil($x_ratio $originalHeight);
    $newWidth $maxWidth;
    } else {
    $newWidth ceil($y_ratio $originalWidth);
    $newHeight $maxHeight;
    }

    $src $imageCreateFunction($image);
    $dst imagecreatetruecolor($newWidth$newHeight);

    // Resample
    $thumbnail imagecopyresampled($dst$src0000$newWidth$newHeight$originalWidth$originalHeight);
    unset(
    $image);
    // Output


    Header('Content-type: image/' $fileType);
    $imageOutputFunction($dst);

    ImageDestroy($src);
    ImageDestroy($dst);

    ?>

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by saunders1989 View Post
    here is imageResize:
    Sorry, I meant image.php

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry i missunderstood what you wanted ill give you both: here is the thumbnails page:

    PHP Code:
    <?php

    /* set up the database connection because real_escape_string() relies on an open connection to work */

    $dbLink = new mysqli('localhost''root''''gallery');
    if(
    mysqli_connect_errno()) {
    die(
    "MySQL connection failed: "mysqli_connect_error());
    }

    /* check that $_GET['gallery'] has a value and the value is numeric - ie someone hasn't changed the url to just gallery.php or tried to be clever & entered gallery.php?gallery=bob */

    if(isset($_GET['gallery']) && is_numeric($_GET['gallery'])) {
    /* $_GET['gallery'] passes our test, so perform security functions on it & assign it to the $gallery variable */
    $gallery $dbLink->real_escape_string($_GET['gallery']);
    } else {
    /* it doesn't exist or it isn't numeric so assign a default value so our query below always gets constructed properly & safely */
    $gallery 1;
    }

    /* construct the query */
    $query "SELECT id, name, size, image_path FROM images WHERE gallery_type_id = $gallery";

    /* run the query */
    $result $dbLink->query($query);

    /* loop through the results & display the images */
    while ($row $result->fetch_assoc()) {
        
    echo 
    "<a href=\"image.php?id=" $row['id'] . "\">";
    echo 
    "<img src=\"imageResize.php?imageFilename=" $row['name'] . "&maxHeight=120,&maxWidth=150\" />\n";
    echo 
    "</a>\n";

    }        
    ?>
    and here is image.php

    PHP Code:
    <?php
    $dbLink 
    = new mysqli('localhost''root''''gallery');
    if(
    mysqli_connect_errno()) {
    die(
    "MySQL connection failed: "mysqli_connect_error());
    }

    if(isset(
    $_GET['id']) && is_numeric($_GET['id'])) {
    /* $_GET['id'] passes our test, so perform security functions on it & assign it to the $id variable */
    $id$dbLink->real_escape_string($_GET['id']);
    } else {
    /* it doesn't exist or it isn't numeric so assign a default value so our query below always gets constructed properly & safely */
    $id 1;
    }


    /* construct the query */
    $query "SELECT name, size, image_path FROM images WHERE id = $id";

    /* run the query */
    $result $dbLink->query($query);

    while(
    $row $result->fetch_row()) {
    echo 
    "<img src=\"" $row[2] . "\" />"// you could also play with the other $row[] variables returned by the query here.
    }

    ?>

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you try what i said before in image.php inside the while loop?
    - Find the width and height of the image (original).
    PHP Code:
    list($width$height) = getimagesize($row[2]); 
    - Check if the image is bigger than the post card (or your default size) and use imageResize.php and pass the width and height if it is bigger one.
    PHP Code:
    if($width $postcardwidth || $height $postcardheight){
        echo 
    "<img src=\"imageResize.php?imageFilename=" $row[2] . "&maxHeight=" $height "&maxWidth=" $width "\" />\n"
    }
    else{
        echo 
    "<img src=\"" $row[2] . "\" />"

    PS: You don't have to use while loop here in image.php because you are getting a single record/row.
    PHP Code:
    $row $result->fetch_row();
    echo 
    "<img src=\"" $row[2] . "\" />"
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    would the new code be:

    PHP Code:
    <?php

    $dbLink 
    = new mysqli('localhost''root''''gallery');

    if(
    mysqli_connect_errno()) {

    die(
    "MySQL connection failed: "mysqli_connect_error());

    }



    if(isset(
    $_GET['id']) && is_numeric($_GET['id'])) {

    /* $_GET['id'] passes our test, so perform security functions on it & assign it to the $id variable */

    $id$dbLink->real_escape_string($_GET['id']);

    } else {

    /* it doesn't exist or it isn't numeric so assign a default value so our query below always gets constructed properly & safely */

    $id 1;

    }





    /* construct the query */

    $query "SELECT name, size, image_path FROM images WHERE id = $id";



    /* run the query */

    $result $dbLink->query($query);



    while(
    $row $result->fetch_row()) {
    list(
    $width$height) = getimagesize($row[2]); 


    }

    if(
    $width $postcardwidth || $height $postcardheight){
        echo 
    "<img src=\"imageResize.php?imageFilename=" $row[2] . "&maxHeight=" $height "&maxWidth=" $width "\" />\n"
    }
    else{
        echo 
    "<img src=\"" $row[2] . "\" />"





    ?>
    i just dont get what the variable postcardwidth and height are about?
    can you just explain a little more please

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    What rajug is saying, is that you can check the actual image size against the max image size you want to use, and if the image is bigger you call the resize script.
    In his script, he uses two variables that would contain the post card size you talked about in your OP, but you can of course use any size you want.

    I think there's a little error in his code. It should be:
    Code PHP:
    $row = $result->fetch_row();
    list($width, $height) = getimagesize($row[2]); 
    if ($width > $maxwidth || $height > $maxheight) {
      echo "<img src=\"imageResize.php?imageFilename=" . $row[2] . "&maxHeight=" . $maxheight . "&maxWidth=" . $maxwidth . "\" />\n"; 
    } else {
      echo "<img src=\"" . $row[2] . "\" />"; 
    }

  12. #12
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh okay. so i can take out the stuff i placed in and literally have this?

    PHP Code:
    <?php



    $dbLink 
    = new mysqli('localhost''root''''gallery');



    if(
    mysqli_connect_errno()) {



    die(
    "MySQL connection failed: "mysqli_connect_error());



    }







    if(isset(
    $_GET['id']) && is_numeric($_GET['id'])) {



    /* $_GET['id'] passes our test, so perform security functions on it & assign it to the $id variable */



    $id$dbLink->real_escape_string($_GET['id']);



    } else {



    /* it doesn't exist or it isn't numeric so assign a default value so our query below always gets constructed properly & safely */



    $id 1;



    }











    /* construct the query */



    $query "SELECT name, size, image_path FROM images WHERE id = $id";







    /* run the query */



    $result $dbLink->query($query);

    $row $result->fetch_row();
    list(
    $width$height) = getimagesize($row[2]);
    if (
    $width $maxwidth || $height $maxheight) {
      echo 
    "<img src=\"imageResize.php?imageFilename=" $row[2] . "&maxHeight=" $maxheight "&maxWidth=" $maxwidth "\" />\n";
    } else {
      echo 
    "<img src=\"" $row[2] . "\" />";
    }

  13. #13
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    You'll have to define $maxwidth and $maxheight somewhere.

  14. #14
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do i not define them in imageResize though?

    or i can do this:
    Last edited by saunders1989; Feb 10, 2010 at 10:11. Reason: add code

  15. #15
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    No, you pass them to imageResize.php as parameters in the query string ("&maxHeight=" . $maxheight . "&maxWidth=" . $maxwidth).
    In imageResize.php, if no parameters are passed, a default height and width is used. But you are passing them, so if you don't give them a value, it won't work. Of course, you can also try calling imageResize.php without passing the height and width parameters.

  16. #16
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry my bad forgot to add the code in the other post:

    PHP Code:
    ?php

    $dbLink 
    = new mysqli('localhost''root''''gallery');

    if(
    mysqli_connect_errno()) {


    die(
    "MySQL connection failed: "mysqli_connect_error());


    }


    if(isset(
    $_GET['id']) && is_numeric($_GET['id'])) {

    /* $_GET['id'] passes our test, so perform security functions on it & assign it to the $id variable */

    $id$dbLink->real_escape_string($_GET['id']);

    } else {

    /* it doesn't exist or it isn't numeric so assign a default value so our query below always gets constructed properly & safely */
    $id 1;
    }

    /* construct the query */
    $query "SELECT name, size, image_path FROM images WHERE id = $id";

    /* run the query */
    $result $dbLink->query($query);

    $maxheight=300;
    maxwidth=250;


    $row $result->fetch_row();

    list(
    $width$height) = getimagesize($row[2]);

    if (
    $width $maxwidth || $height $maxheight) {

      echo 
    "<img src=\"imageResize.php?imageFilename=" $row[2] . "&maxHeight=" $maxheight "&maxWidth=" $maxwidth "\" />\n";

    } else {

      echo 
    "<img src=\"" $row[2] . "\" />";



  17. #17
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by saunders1989 View Post
    do i not define them in imageResize though?

    or i can do this:
    If you dont need those in imageResize then you dont have to define them otherwise you have to define them in both files i guess.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  18. #18
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im really sorry. your last post has confused me.

    in imageResize i have a maxwidth and a maxheight set. i will change that as they are set at thumbnail size. but what your tryingto say has confused me

  19. #19
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by saunders1989 View Post
    or i can do this:
    Yes you can. Watch out: there is a $ missing

  20. #20
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,399
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by saunders1989 View Post
    im really sorry. your last post has confused me.

    in imageResize i have a maxwidth and a maxheight set. i will change that as they are set at thumbnail size. but what your tryingto say has confused me
    You do not have a maxwidth and maxheight set in imageResize.
    Code PHP:
    if(!$_GET['maxWidth']) {
      $maxWidth = 100;
    } else {
      $maxWidth = $_GET['maxWidth'];
    }
    This is a piece of imageResize. It says: if you passed me a parameter called maxWidth, then I'll use its value as max width. If you didn't pass me such a parameter, I'll use 100 as max width.

    So you have two choices.
    1) Define a value for maxWidth and maxHeight in the script that calls imageResize (as you did in the last script you posted: $maxheight=300; $maxwidth=250; )
    2) Call imageResize without the maxWidth and maxHeight parameters:
    Code PHP:
    echo "<img src=\"imageResize.php?imageFilename=" . $row[2] . "\" />\n";

  21. #21
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the second option doesnt work, ive tried that method first but it doesnt display my image at all.

    is the next step to use the code i tried writing?


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
  •