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 4 of 4

Thread: Image resizer

  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Image resizer

    Hey guys, I have a little script for image re sizing, getting all kind of errors when I run it.

    The uploader works fine on its own, but when i include the re sizer, it will still upload my original image but doesn't re size the other images.

    Here's my form and upload script:
    Code:
    <form name="uploader" method="POST" enctype="multipart/form-data">
    <p>Image Name:</p>
    <p><input type="text" name="title" class="imageUploader" /></p>
    <p>Image Description:</p>
    <p><textarea name="message" class="upload_desc"></textarea></p>
    <p>File Name:</p>
    <p><input type="file" name="file" class="image_upload" /></p>
    <p><input type="submit" value="Upload" class="upload_submit" /></p>
    </form>
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
            if((
    $_FILES['file']['type'] == 'image/gif')
            || (
    $_FILES['file']['type'] == 'image/jpeg')
            || (
    $_FILES['file']['type'] == 'image/png')
            || (
    $_FILES['file']['type'] == 'image/pjpeg')){
                if(
    $_FILES['type']['error'] > 0){
                    echo 
    "Return Code: " $_FILES['file']['error'] . "<br>";
                }else{
                    
    $title $_POST['title'];
                    
    $desc $_POST['message'];
                    
    $original $_FILES['file']['name'];
                    
    $ext pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
                    
                    
    $newfiles md5(time()) . "." $ext;
                    
                    if(
    move_uploaded_file($_FILES['file']['tmp_name'], "./upload/".$original)){
                        
    mysql_query("INSERT INTO `img` (`title`, `desc`, `img_s`, `img_m`, `img_org`) VALUES ('".$title."', '".$desc."', '".$newfiles."', '".$newfiles."', '".$original."')");
                        include(
    "image_resize.php");
                        echo 
    "<p>Upload successful!</p>";
                    }else{
                        echo 
    "<p>Failed to upload!</p>";    
                    }
                }
            }else{
                echo 
    "<p>Invalid file type!</p>";
            }
        } 
    Here is the image re sizer:
    PHP Code:
    function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { 
        
    $s_path trim($s_path); 
        
    $o_path trim($o_path); 
        
    $save $s_path $save
        
    $file $o_path $file
        
    $ext strtolower(end(explode('.',$save))); 
        list(
    $width$height) = getimagesize($file) ; 
        if((
    $width>$t_w) OR ($height>$t_h)) { 
            
    $r1 $t_w/$width
            
    $r2 $t_h/$height
            if(
    $r1<$r2) { 
                
    $size $t_w/$width
            }else{ 
                
    $size $t_h/$height
            } 
        }else{ 
            
    $size=1
        } 
        
    $modwidth $width $size
        
    $modheight $height $size
        
    $tn imagecreatetruecolor($modwidth$modheight) ; 
        switch (
    $ext) { 
            case 
    'jpg'
            case 
    'jpeg'
                
    $image imagecreatefromjpeg($file) ; 
                break; 
            case 
    'gif'
                
    $image imagecreatefromgif($file) ; 
                break; 
            case 
    'png'
                
    $image imagecreatefrompng($file) ; 
                break; 
        } 
        
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height) ; 
        
    imagejpeg($tn$save100) ; 
        return; 
    }

    $file $newfiles;
    $filename $dir $file;

    /* Small Picture */
    $save $filename;
    $t_w 150;
    $t_h 150;
    $o_path "upload/"
    $s_path "upload/small/";

    Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

    /* Photogallery Picture */
    $t_w 250;
    $t_h 165;
    $o_path "upload/"
    $s_path "upload/medium/";

    Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); 
    Just some of the errors if you want to see them:

    Warning: getimagesize(upload/0906d5ca9efc947407f10ed54ba56394.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\htdocs\admin\image_resize.php on line 8

    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\xampp\htdocs\admin\image_resize.php on line 22

    Warning: imagecreatefromjpeg(upload/0906d5ca9efc947407f10ed54ba56394.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\xampp\htdocs\admin\image_resize.php on line 26

    Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\admin\image_resize.php on line 35

    Warning: imagejpeg() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\admin\image_resize.php on line 36

    Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\admin\image_resize.php on line 7

    Warning: getimagesize(upload/0906d5ca9efc947407f10ed54ba56394.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\htdocs\admin\image_resize.php on line 8

    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\xampp\htdocs\admin\image_resize.php on line 22

    Warning: imagecreatefromjpeg(upload/0906d5ca9efc947407f10ed54ba56394.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\xampp\htdocs\admin\image_resize.php on line 26

    Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\admin\image_resize.php on line 35

    Warning: imagejpeg() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\admin\image_resize.php on line 36

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    Those scripts are not written to work together, so tacking the second one on just won't cut it. And to me it's not a 'little script', there's a lot more there than is needed.

    For the Resize_Image function, I see that none of it's parameters are defined in the first script, so it can't find $file, which it needs in the first step, so all the others fall like dominoes. I don't use GD, I use Imagemagick, so I don't have a ready made script for you.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah ok, so can I edit this script to work with my uploader?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    The number of errors you have listed there is going to put off anyone from assisting here really.

    You've also posted the function and the other php code but not told us which one of each is in what file name. How do I know what image_resize.php on line 8 is referring to? - The function code or the php code above it?

    To be honest, I think you're going to be better off dumping that code and using this from white-hat-web-design in the UK:

    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;
       }
    }
    ?>
    I can't really think of anything to write here now...


  •  

    Posting Permissions

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