Results 1 to 3 of 3

Thread: Image Resizing

  1. #1
    Join Date
    Nov 2006
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Image Resizing

    How do I make an image resize to a certain size (in pixels or size %) using PHP?
    I found a script on the PHP website that looks like this:
    PHP Code:
    <?php
    // The file
    $filename 'test.jpg';

    // Set a maximum height and width
    $width 200;
    $height 200;

    // Content type
    header('Content-type: image/jpeg');

    // Get new dimensions
    list($width_orig$height_orig) = getimagesize($filename);

    $ratio_orig $width_orig/$height_orig;

    if (
    $width/$height $ratio_orig) {
       
    $width $height*$ratio_orig;
    } else {
       
    $height $width/$ratio_orig;
    }

    // Resample
    $image_p imagecreatetruecolor($width$height);
    $image imagecreatefromjpeg($filename);
    imagecopyresampled($image_p$image0000$width$height$width_orig$height_orig);

    // Output
    imagejpeg($image_pnull100);
    ?>
    But how do I make it so that I put all that into a single PHP file then just call to it(<?php include() ?>) in an individual PHP page file?

  2. #2
    Join Date
    Nov 2006
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    BUMP. Any help would be appreciated. (I can't even get that script on its own to work...)

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You wouldn't use an include statement for this.

    I'd recommend, for example, assuming you want a thumbnail for images (like I have done), creating thumbnail.php and using a GET variable to set the image.

    Replace:
    $filename = 'test.jpg';
    With:
    $filename = $_GET['img'];

    And use:
    ...thumbnail.php?img=filename.jpg

    And that can be changed each time.

    Then, just use normal image tags to include that as an IMAGE.

    Remember, a php file outputting an image is just like a .jpg/.gif/.png and should be treated as such. Including it into another php file would just screw things up, with image data and headers being output within html data and headers. a php file outputting a valid image can be used within an <img> tag, viewed in a browser as an image, used as a background, etc etc.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •