SitePoint Sponsor

User Tag List

Page 2 of 6 FirstFirst 123456 LastLast
Results 26 to 50 of 139
  1. #26
    Anonymous
    SitePoint Community Guest
    all this function does is change the width and height in php, cmon man, a whole page dedicated to changing the width and height to be sent back ? you can do that manually or using js, a real useful function would be ACTUALLY resizing the image. so you can load say your original digital 5mb file size of all your socks ina folder, and php will take it and resize them to display and actually resize the image so the browser doesnt download 5mb size thumbnails forever

    this page acting like resizing the width= and height= value is so complicated... cmon man..

  2. #27
    beeplogic
    SitePoint Community Guest
    For Greater Justice. Since the code in this article is not likely to be used in production here is a function that will resize an image in to a given X (width) and Y (height) value. This function requires the GD extension to be installed.

    function restrainImage($path,$maxWidth,$maxHeight,$dst=null){
    $imageInfo = getimagesize($path);
    $width = $imageInfo[0];
    $height = $imageInfo[1];
    $types = array (1 => 'gif', 'jpeg', 'png');

    $x_ratio = $maxWidth / $width;
    $y_ratio = $maxHeight / $height;

    if( $width <= $maxWidth && $height <= $maxHeight ){
    $newWidth = $width;
    $newHeight = $height;
    }elseif( ($x_ratio * $height) < $maxHeight){
    $newWidth = $maxWidth;
    $newHeight = ceil($x_ratio * $height);
    }else{
    $newWidth = ceil($y_ratio * $width);
    $newHeight = $maxHeight;
    }
    $image = call_user_func('imagecreatefrom'.$types[$imageInfo[2]],$path);
    $resized = imagecreatetruecolor($newWidth,$newHeight);
    imagecopyresized($resized,$image,0,0,0,0,$newWidth,$newHeight,$width,$height);
    call_user_func('image'.$types[$imageInfo[2]],$resized,($dst!=null?$dst:$path));
    }

    $dst is optional, if left null it will overwrite the original file. Keep in mind there is no error checking in this function and will only work if the file type is a GIF, PNG or JPG. It should give one a general idea of how to go about resizing an image file with PHP.

  3. #28
    setd
    SitePoint Community Guest
    Any reason why this might not be working in IE? I can get it to work fine in Firefox. Would it be the functions itself being the problem or is PHP not affected meaning it could just be an html error?

    Cheers,

  4. #29
    elbotho
    SitePoint Community Guest
    This is a Server-Side script, so it should be a html-error.

  5. #30
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hope someone replies to this.....

    I want to use this function in a dynamic page ie. the images are loaded from a database using a php script so I can't use the print function to display the image as suggested in prevoius threads. anyone know how to achieve this..

    I've put the main part of the code below,

    PHP Code:
    echo "<table class=\"table_test\">\n";
            
        }
    //end of first if
        
        //Display each record
            
    echo "<tr>\n"
            
            
    //get the image size of the picture and load it into an array
                
    $mysock getimagesize("rivcms/news/images/image_{$row['upload_id']}06.jpg");
                
    //display the resized image on the screen. 
                
    echo "<td><img src=\"rivcms/news/images/image_{$row['upload_id']}06.jpg\" WHAT DO I DO HERE imageResize($mysock[0]$mysock[1], 75) />\n";

            
            echo 
    "<td>{$row['t']}</td>\n";
            echo 
    "<td>{$row['d']}</td>\n";
            echo 
    "</tr>\n";
        
        
    $first FALSE//One record has been returned
        

    thanks!

  6. #31
    gus
    SitePoint Community Guest
    You could do it two ways, from what I can see.

    1) // drop out of PHP
    ?>

    <td><img src="rivcms/news/images/image_{<?php echo($row['upload_id']);?>06.jpg" <?php imageResize($mysock[0], $mysock[1], 75);?> />

    // go back into PHP
    <?php

    2) echo("<td><img src=\"rivcms/news/images/image_{$row['upload_id']}06.jpg\" ");
    imageResize($mysock[0], $mysock[1], 75);
    echo(" />");

  7. #32
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey guys...
    i'm wondering if there is any good way to resize a bmp image with php
    i'm using imagewbmp from the gd library but all i'm getting a a black resized image...

    any suggestions?

  8. #33
    Casey
    SitePoint Community Guest
    I have a question. I have images stored in a mysql database. When I retrieve them, they're given a URL like getimg.php?id=23243, for example, of course. I found this script does not return the right information for images that do not end in a file name. Is there any modification I can use for the function to get it working with a databased image? Please email me ASAP, csmi27 at gmail dot com, as I probably won't check back here often. Thanks!

  9. #34
    Jordan Bradley
    SitePoint Community Guest
    Well I tried it and it doesn't work. The image is the same size. However I understand how it works. Thanks

  10. #35
    aaxc
    SitePoint Community Guest
    The right code is:

    <?php

    Function imageResize($width, $height, $target) {

    //takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image

    if ($width > $height) {
    $percentage = ($target / $width);
    } else {
    $percentage = ($target / $height);
    }

    //gets the new value and applies the percentage, then rounds the value
    $width = round($width * $percentage);
    $height = round($height * $percentage);

    //returns the new sizes in html image tag format...this is so you can plug this function inside an image tag and just get the

    return "width=\"$width\" height=\"$height\"";

    }

    //define the image source
    $image_link = "http://my.image.com/pic.jpg";

    //get the image size of the picture and load it into an array
    $mysock = getimagesize($image_link);

    ?>

    <!-using a standard html image tag, where you would have the
    width and height, insert your new imageResize() function with
    the correct attributes -->

    <img src="<?php echo $image_link; ?>" <?php echo imageResize($mysock[0], $mysock[1], 150); ?>>

    He just forgot to put the echo function on retur, but this code still uses a simple html resize function and the picture comes out crapy anyways :(

  11. #36
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DatraxMada
    Here is a function which is simpler to call and doesnt mix the PHP and IMG tags.

    <?php
    function imageResize($image, $target, $alt=NULL) {

    //Gets the width and height of the image and outputs it as $theimage[0] (width) and $theimage[1] (height)
    $theimage = getimagesize($image);
    $width = $theimage[0];
    $height = $theimage[1];

    //takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image

    if ($width > $height) {
    $percentage = ($target / $width);
    } else {
    $percentage = ($target / $height);
    }

    //gets the new value and applies the percentage, then rounds the value
    $width = round($width * $percentage);
    $height = round($height * $percentage);

    //Returns the new sizes inside an image tag so you can call it with "imageResize("images/sock001.jpg", "Some alt text", $target)"

    echo "<img src=\"$image\" width=\"$width\" height=\"$height\" alt=\"$alt\">";
    }
    ?>

    <!- Now, you can simply use your imageResize function instead of mixing up your PHP with an IMG tag -->

    <?php imageResize("images/sock001.jpg", 50, "Oh what a beautiful sock"); ?>

    How would I get this code to work on images held in a directory.

    eg - i would like all my files to be limited to 450 pixels wide when they are displayed

    also where would i put this script if my current code is


    //This is the query
    $query = "SELECT * FROM upload2";
    $result = mysql_query ($query);

    #First print the opening tag...
    echo '<table width="800" border="0" cellspacing="0" cellpadding="0">';

    while ($row = mysql_fetch_assoc ($result)) {
    $text = $row['id'];
    $url = $row['path'];
    $description = $row['comment'];


    #this will print a new row for each photo..
    echo "
    <tr>
    <td align=\"center\"><img src=\"$url\" alt=\"\"><br>$url</td>
    <td align=\"left\" valign=\"middle\">$description</td>
    </tr>";

    }
    // This is to close database
    mysql_close($conn);
    echo "</table>";
    ?>


    Hope you can help

  12. #37
    Matt Faris
    SitePoint Community Guest
    I've used this script - thank you. I'm however having a problem with the images that are larger than the parameter passed in the function. These are photographs which are quite large. When I try to browse to the images folder to view the image I get a message stating "The image “resize.jpg” cannot be displayed, because it contains errors."

    Any ideas

  13. #38
    SitePoint Member Shobhit's Avatar
    Join Date
    Oct 2005
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP functions imagecopyresampled() OR imagecopyresized() can also be used for smoothly resizing images.

    @QuizToon: you can use the following in your code in place of img tag:

    $size = getimagesize("$url");
    $height = $size[1];
    $width = $size[0];
    if ($width > 450)
    {
    $width = 450;
    $percent = ($size[0] / $width);
    $height = ($size[1] / $percent);
    }
    @echo "<img src=\"$url\" height=\"$height\" width=\"$width\" border=0 alt=\"\" />";
    }
    else
    return "Picture Unavailable";
    }

  14. #39
    Salis
    SitePoint Community Guest
    This is an awesome code. I did how ever notice one error with the image size. They were the same size, then I noticed the the echo ws missing. So instead of:
    <img src="myimage.jpg" <?php imageResize($mysock[0], $mysock[1], 150);?> />

    Try:

    <img src="gallery/abstract1600_3_084.jpg" <?php echo imageResize($mysock[0], $mysock[1], 150);?> />

    Thanks for the code!

  15. #40
    Katarina
    SitePoint Community Guest
    Thanks so much for the code!! Uf, I am running out of time with my web-page development and I was lucky to find your code!
    I would like to add just one more (not that important thing)
    you can write this code:
    echo "<img src=\"$url\" height=\"$height\" width=\"$width\" border=0 alt=\"\" />";
    like this as well:
    echo "<img src='$url' height='$height' width='$width' border=0 alt='' />";
    K

  16. #41
    Daniel Cottrell
    SitePoint Community Guest
    This will increase your overhead when used on lots of images. I was looking for a method of resizing and saving them permenantly.

  17. #42
    Designer/Developer FarooqAzam's Avatar
    Join Date
    May 2004
    Location
    Pakistan
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot. Very Helpful tutorial.

  18. #43
    MAN
    SitePoint Community Guest
    Nice trick, but it is not efficient, because the real goal is to make REAL, saved, thumbnails that will make the website FAST, and if the visitor wants to see the full sized image he will click on the provided link.

    Can you put a pHp code with real image RESIZE function???

  19. #44
    SitePoint Zealot chrisdpucci's Avatar
    Join Date
    Dec 2006
    Location
    On the internets
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is actually a php function that does this exact thing, (create dynamic thumbnails in real-time), although the way it does it is way more efficient. It's called phpthumbs and it works very well.

    http://phpthumb.sourceforge.net/

  20. #45
    aaa
    SitePoint Community Guest
    Perfect! Just what I need! Tnx!

  21. #46
    Steven Haddock
    SitePoint Community Guest
    I have an idea for a website which might be far fetched but I need a solution.

    I want to use PHP to take a screenshot of a website I tell it to, add this to a database possibly, and be resized by PHP. I need screenshots of maybe up to 90 websites for a website I want to create for uni, but I certainly do not want to have to take these screenshots myself, and have to obtain these maybe on a weekly basis so I want somethign to do it for me, resize and store...

    Any suggestions? email to haddock-s@ulster.ac.uk please

  22. #47
    mufaddal
    SitePoint Community Guest
    not working

  23. #48
    Alishah Novin
    SitePoint Community Guest
    Not very practical, like you say. The idea of a thumbnail is to give the user a quick idea of what something will look like without requiring them to download a large file... The other issue is that having a browser resize an image, even when maintaining aspect ratios, is a poor choice for quality.

  24. #49
    Alex
    SitePoint Community Guest
    Resizing images with the browser is a very bad ideea. you should create a thumbanil when uploading a new photo :)

  25. #50
    Andreas
    SitePoint Community Guest
    or use phpThumb!


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
  •