SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed with image resize script

    Hi,

    I found the following script for resizing images on the fly and I'm trying to understand why it isn't working (or what it is that I'm doing wrong to cause the function not to work).

    Any help appreciated.

    I have the following folder structure...

    index.php
    images/test.jpg

    The code for index.php is at the bottom of this post. You'll see i'm passing in as arguments the path to the existing text.jpg and also the path of the new image I'm expecting it to generate but I get the following error message...

    PHP Code:
    Warningimagecreatefromjpeg(images/newfile.jpg) [function.imagecreatefromjpeg]: failed to open streamNo such file or directory in C:\Apache2\htdocs\test\index.php on line 14

    Warning
    imagesx() expects parameter 1 to be resourceboolean given in C:\Apache2\htdocs\test\index.php on line 23

    Warning
    imagesy() expects parameter 1 to be resourceboolean given in C:\Apache2\htdocs\test\index.php on line 24 
    PHP Code:
    /**
         * @filename - path to the image
         * @tmpname - temporary path to thumbnail
         * @xmax - max width
         * @ymax - max height
         */
        
    function resize_image($filename$tmpname$xmax$ymax)
        {
            
    $ext explode("."$filename);
            
    $ext $ext[count($ext)-1];
        
            if(
    $ext == "jpg" || $ext == "jpeg") {
                
    $im imagecreatefromjpeg($tmpname);
            }
            elseif(
    $ext == "png") {
                
    $im imagecreatefrompng($tmpname);
            }
            elseif(
    $ext == "gif") {
                
    $im imagecreatefromgif($tmpname);
            }
        
            
    $x imagesx($im);
            
    $y imagesy($im);
        
            if(
    $x <= $xmax && $y <= $ymax) {
                return 
    $im;
            }
        
            if(
    $x >= $y) {
                
    $newx $xmax;
                
    $newy $newx $y $x;
            }
            else {
                
    $newy $ymax;
                
    $newx $x $y $newy;
            }
        
            
    $im2 imagecreatetruecolor($newx$newy);
            
    imagecopyresized($im2$im0000floor($newx), floor($newy), $x$y);
            return 
    $im2;
        }
        
        echo 
    "<img src='".resize_image("images/test.jpg""images/newfile.jpg"100100)."'>"
    Now I appreciate that this script probably isn't the best way of doing things but I'm just looking at proof of concept to help me understand how it is done.

    Again, any help appreciated.

    Kind regards,

    M.

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Resizing images on the fly is the best way to hung your server up.
    Image is not text. It need huge amount of RAM and CPU power to transform.

    And this code is very silly. Better find another one.

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you use the exsisting libs for thumnails.
    Here is one


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
  •