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

    PHP help Resize an image

    I have images in a directory that are uploaded from smart phones.
    I want to display the latest image uploaded on my home page.
    I have the following code which works fine except it displays the image in it's original size.
    I need the image to be displayed at 250px wide while maintaining the ratio.

    This is what I have so far:

    <?php
    $dir = '../images/catches/test/';
    $base_url = '../images/catches/test/';
    $newest_mtime = 0;
    $show_file = 'BROKEN';
    if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
    if (($file != '.') && ($file != '..')) {
    $mtime = filemtime("$dir/$file");
    if ($mtime > $newest_mtime) {
    $newest_mtime = $mtime;
    $show_file = "$base_url/$file";
    }
    }
    }
    }
    print '<img src="' .$show_file. '" alt="Image Title Here">';
    ?>


    Can anyone help me modify this to resize? I have tried but it is well beyond my capabilities.

    Thanks

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Posts
    459
    Thanks
    0
    Thanked 20 Times in 20 Posts
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • Users who have thanked c1lonewolf for this post:

    laurenor (03-27-2014)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,339
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Courtesy of white hat web design:

    PHP Code:
    <?
    /*
    * File: SimpleImage.php
    * Author: Simon Jarvis
    * Copyright: 2006 Simon Jarvis
    * Date: 08/11/06
    * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details:
    * http://www.gnu.org/licenses/gpl.html
    *
    */

    class SimpleImage {

       var 
    $image;
       var 
    $image_type;

       function 
    load($filename) {
          
    $image_info getimagesize($filename);
          
    $this->image_type $image_info[2];
          if( 
    $this->image_type == IMAGETYPE_JPEG ) {
             
    $this->image imagecreatefromjpeg($filename);
          } elseif( 
    $this->image_type == IMAGETYPE_GIF ) {
             
    $this->image imagecreatefromgif($filename);
          } elseif( 
    $this->image_type == IMAGETYPE_PNG ) {
             
    $this->image imagecreatefrompng($filename);
          }
       }
       function 
    save($filename$image_type=IMAGETYPE_JPEG$compression=100$permissions=null)
          {
             switch (
    $image_type)
                {
                case 
    IMAGETYPE_JPEG:
                case 
    'image/jpeg':
                   
    imagejpeg($this->image,$filename,$compression);
                   break;
                case 
    IMAGETYPE_GIF:
                case 
    'image/gif':
                   
    imagegif($this->image,$filename);
                   break;
                case 
    IMAGETYPE_BMP:
                case 
    'image/bmp':
                   
    imagewbmp($this->image,$filename);
                   break;
                }
                
             if( 
    $permissions != null) {
                
    chmod($filename,$permissions);
             }
          }
       function 
    output($image_type=IMAGETYPE_JPEG)
          {
             switch (
    $image_type)
                {
                case 
    IMAGETYPE_JPEG:
                case 
    'image/jpeg':
                   
    imagejpeg($this->image);
                   break;
                case 
    IMAGETYPE_GIF:
                case 
    'image/gif':
                   
    imagegif($this->image);
                   break;
                case 
    IMAGETYPE_BMP:
                case 
    'image/bmp':
                   
    imagewbmp($this->image);
                   break;
                }
          }
          
       function 
    getWidth() {
          return 
    imagesx($this->image);
       }
       function 
    getHeight() {
          return 
    imagesy($this->image);
       }
       function 
    resizeToHeight($height) {
          
    $ratio $height $this->getHeight();
          
    $width $this->getWidth() * $ratio;
          
    $this->resize($width,$height);
       }
       function 
    resizeToWidth($width) {
          
    $ratio $width $this->getWidth();
          
    $height $this->getheight() * $ratio;
          
    $this->resize($width,$height);
       }
       function 
    scale($scale) {
          
    $width $this->getWidth() * $scale/100;
          
    $height $this->getheight() * $scale/100;
          
    $this->resize($width,$height);
       }
       function 
    resize($width,$height) {
          
    $new_image imagecreatetruecolor($width$height);
          
    imagecopyresampled($new_image$this->image0000$width$height$this->getWidth(), $this->getHeight());
          
    $this->image $new_image;
       }
    }
    ?>
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    laurenor (03-27-2014)


  •  

    Posting Permissions

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