Results 1 to 3 of 3

Thread: resize image in php

  1. #1
    Join Date
    Aug 2013
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default resize image in php

    I want to upload, recreate and resize image to the widths and height
    Code:
    $x35MaxWidth  = 35; //35x35 width
    $x35MaxHeight  = 35; //35x35 Height

    using code below. can someone help me

    Code:
    <?php
    
    /*
    * File: SimpleImage.php
    * Author: Simon Jarvis
    * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
    */
    
    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=750, $permissions=null) {
    
          if( $image_type == IMAGETYPE_JPEG ) {
             imagejpeg($this->image,$filename,$compression);
          } elseif( $image_type == IMAGETYPE_GIF ) {
    
             imagegif($this->image,$filename);
          } elseif( $image_type == IMAGETYPE_PNG ) {
    
             imagepng($this->image,$filename);
          }
          if( $permissions != null) {
    
             chmod($filename,$permissions);
          }
       }
       function output($image_type=IMAGETYPE_JPEG) {
    
          if( $image_type == IMAGETYPE_JPEG ) {
             imagejpeg($this->image);
          } elseif( $image_type == IMAGETYPE_GIF ) {
    
             imagegif($this->image);
          } elseif( $image_type == IMAGETYPE_PNG ) {
    
             imagepng($this->image);
          }
       }
       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->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
          $this->image = $new_image;
       }      
    
    }
    ?>

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    The source site has usage instructions http://www.white-hat-web-design.co.u...ages-with-php/

    To resize to 35 x 35, it looks like you would use this snippet;
    PHP Code:
    <?php
       
    include('SimpleImage.php');
       
    $image = new SimpleImage();
       
    $image->load('picture.jpg');
       
    $image->resize(35,35);
       
    $image->save('picture2.jpg');
    ?>
    There is an example on the same page that shows how to integrate it with an upload script although the script itself doesn't provide an upload function directly so you would need to source that elsewhere.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Feb 2015
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you for the information. PHP noob here.

Similar Threads

  1. Container & Image resize on browser resize
    By mickkmg in forum JavaScript
    Replies: 0
    Last Post: 06-11-2012, 11:59 AM
  2. Replies: 3
    Last Post: 02-21-2012, 12:03 PM
  3. Replies: 6
    Last Post: 11-06-2009, 10:32 PM
  4. Need Help with image Resize
    By captainjustin in forum PHP
    Replies: 2
    Last Post: 08-28-2009, 08:25 AM
  5. What is the best way to resize an image?
    By james438 in forum PHP
    Replies: 2
    Last Post: 05-01-2009, 04:57 AM

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
  •