Solved

php  jpg dynamic server side image resize script problem

Posted on 2004-09-11
5
329 Views
Last Modified: 2008-02-01
I am trying to use the code below to resize this image...
This script works on another server, but when I went on to this server it doesnt work.
PLEASE HELP, Ive gotta finish this site by the end of the weekend. Laura Bush is visiting it. =)

It seems to stop executing on the line that says...
$this->_imgFinal = @ImageCreateTrueColor($nx, $ny);
under the _cropsize function...

script I am executing:
http://www.flagladyinc.com/00image_thumbnail.php?gd=2&src=solarmaxamericanflag.jpg&maxw=220

code:

/////////////////////////


<?php
     
//call image resize function  
image_resize($_GET["src"], $_GET["maxw"]);


//this page includes class and functions to dynamically resize and/or crop images


//Functions to USE in your pages
function image_resize($src,$maxX) {
      //$_SERVER["DOCUMENT_ROOT"].
      $src='/home/httpd/vhosts/flagladyinc.com/httpdocs/images/items/'.$src;
      
      $cc2 = new canvasCrop();
      $cc2->loadImage($src);
      $cc2->ReSize($maxX,ccCENTRE);
      $cc2->showImage('jpg',65);
}



define("ccTOPLEFT",     0);
define("ccTOP",         1);
define("ccTOPRIGHT",    2);
define("ccLEFT",        3);
define("ccCENTRE",      4);
define("ccCENTER",      4);
define("ccRIGHT",       5);
define("ccBOTTOMLEFT",  6);
define("ccBOTTOM",      7);
define("ccBOTTOMRIGHT", 8);


class canvasCrop
{
    var $_imgOrig;
    var $_imgFinal;

    var $_showDebug;
    var $_gdVersion;
   

    /**
    * @return canvasCrop
    * @param bool $debug
    * @desc Class initializer
    */
    function canvasCrop($debug = false)
    {
        $this->_showDebug = ($debug ? true : false);
        $this->_gdVersion = (function_exists('imagecreatetruecolor')) ? 2 : 1;
    }


    /**
    * @return bool
    * @param string $filename
    * @desc Load an image from the file system - method based on file extension
    */
    function loadImage($filename)
    {
        //check for file if file does not exist, load nopic
            if (!@file_exists($filename))
        {
                  echo 'file does not exist';
                  $filename=$_SERVER["DOCUMENT_ROOT"].'/images/nopic.jpg';
                  $ext  = strtolower($this->_getExtension($filename));
                  $func = "imagecreatefrom$ext";
                  $this->_imgOrig = @$func($filename);
        }else{
                  echo 'file exist';
                  $ext  = strtolower($this->_getExtension($filename));
                  $func = "imagecreatefrom$ext";
                  $this->_imgOrig = @$func($filename);
        }
            
        if ($this->_imgOrig == null)
        {
            $this->_debug('loadImage', 'The image could not be loaded.');
            return false;
        }else{
            echo 'image has been loaded';
            }
            
       
        return true;
    }
   
 
    /**
    * @return bool
    * @param string $type
    * @param int $quality
    * @desc Shows the cropped image without any saving
    */
    function showImage($type , $quality )
    {
       
            if ($this->_imgFinal == null)
        {
            $this->_debug('showImage', 'There is no processed image to show.');
            echo '<br>we do NOT have an image to show';
                  return false;
                  
        }else{
            echo '<br>we do have an image to show';
            }
            
        if ($type == 'png')
        {
            echo @ImagePNG($this->_imgFinal);
                  echo '<br>png type';
            return true;
        }else if ($type == 'jpg' || $type == 'jpeg')
        {
                  echo @ImageJPEG($this->_imgFinal, '', $quality);
            echo '<br>jpg or jpeg type';
                  return true;
        }else
        {
            $this->_debug('showImage', "Could not show the output file as a $type.");
            echo '<br>didnt find proper type';
                  return false;
        }
    }

      

/////////////////////////////////////////
//function to resize image MAX X and Y
///////////////////////////////////
    function ReSize($maxX,$position)
    {
        if ($this->_imgFinal != null) { $this->_imgOrig=$this->_imgFinal; }
            
            $ogx =  @ImageSX($this->_imgOrig);
            $ogy =  @ImageSY($this->_imgOrig);
            echo '<br>image dims:ogx'.$ogx.":ogy".$ogy;
            //create new width
            if ($ogx > $maxX) {
                  $nx = intval ($maxX);
                  $ny = intval ($ogy * ($maxX / $ogx));
            } else {
                  $nx = $ogx;
                  $ny = $ogy;
            }
            echo '<br>create new image dims:nx'.$nx.":ny".$ny;      
            //check for larger height and resize accordingly
            if ($ny > $maxX) {
                  $nx = intval ($ogx * ($maxX / $ogy));
                  $ny = $maxX ;
            }
            echo '<br>height check:nx'.$nx.":ny".$ny;
            return ($this->_cropSize(0, 0, $nx, $ny, $position, 'ReSize',$ogx,$ogy));
            
    }


   

    /**
    * @return bool
    * @param int $ox Original image width
    * @param int $oy Original image height
    * @param int $nx New width
    * @param int $ny New height
    * @param int $position Where to place the crop
    * @param string $function Name of the calling function
    * @desc Creates the cropped image based on passed parameters
    */
    function _cropSize($ox, $oy, $nx, $ny, $position, $function,$ogx,$ogy)
    {
        if ($this->_imgOrig == null)
        {
            $this->_debug($function, 'The original image has not been loaded.');
            echo '<br>The original image has not been loaded.';
                  return false;
        }else{
            echo '<br>original image has been loaded.';
            }
       
            if (($nx <= 0) || ($ny <= 0))
        {
            $this->_debug($function, 'The image could not be cropped because the size given is not valid.');
            echo '<br>The image could not be cropped because the size given is not valid';
                  return false;
        }else{
            echo '<br>image can be cropped.';
            }
        if  ($ogy == 0)  {$ogy =$ny ; }
            if  ($ogx == 0)  {$ogx =$nx ; }
            
            echo '<br>check1:ogx'.$ogx.' :ogy'. $ogy ;
            echo '<br>check2:nx'.$nx.' :ny'.$ny ;
            echo '<br>ImageSY:'.@ImageSY($this->_imgOrig);
            echo '<br>ImageSX:'.@ImageSX($this->_imgOrig);
            if  ($ny > @ImageSY($this->_imgOrig))  {$ny = @ImageSY($this->_imgOrig); }
            if  ($nx > @ImageSX($this->_imgOrig))  {$nx = @ImageSX($this->_imgOrig); }
       
            if ($ox == -1 || $oy == -1) { list($ox, $oy) = $this->_getCopyPosition($nx, $ny, $position); }
        echo '<br>check3:ogx'.$ogx.' :ogy'. $ogy ;
            echo '<br>check4:nx'.$nx.' :ny'.$ny ;
            echo '<br>check5:ox'.$ox.' :oy'.$oy ;
            echo '<br>this->_gdVersion:'.$this->_gdVersion;
            echo '<br>imagecreatetruecolor:'. function_exists('imagecreatetruecolor') ;    
                  $this->_imgFinal = @ImageCreateTrueColor($nx, $ny);
            echo '<br>imgFinalSY:'.@ImageSY($this->_imgFinal);
            echo '<br>imgFinalSX:'.@ImageSX($this->_imgFinal);      
            @ImageCopyResampled($this->_imgFinal, $this->_imgOrig, 0, 0, $ox, $oy, $nx, $ny, $ogx, $ogy);
       
        return true;
    }
      


    /**
    * @return array
    * @param int $nx
    * @param int $ny
    * @param int $position
    * @desc Determines dimensions of the crop
    */
    function _getCopyPosition($nx, $ny, $position)
    {
        $ox = @ImageSX($this->_imgOrig);
        $oy = @ImageSY($this->_imgOrig);
       
        switch($position)
        {
            case ccTOPLEFT:
                return array(0, 0);
            case ccTOP:
                return array(ceil(($ox - $nx) / 2), 0);
            case ccTOPRIGHT:
                return array(($ox - $nx), 0);
            case ccLEFT:
                return array(0, ceil(($oy - $ny) / 2));
            case ccCENTRE:
                return array(ceil(($ox - $nx) / 2), ceil(($oy - $ny) / 2));
            case ccRIGHT:
                return array(($ox - $nx), ceil(($oy - $ny) / 2));
            case ccBOTTOMLEFT:
                return array(0, ($oy - $ny));
            case ccBOTTOM:
                return array(ceil(($ox - $nx) / 2), ($oy - $ny));
            case ccBOTTOMRIGHT:
                return array(($ox - $nx), ($oy - $ny));
        }
    }


 
   

    /**
    * @return string
    * @param string $filename
    * @desc Get the extension of a file name
    */
    function _getExtension($filename)
    {
        $ext  = @strtolower(@substr($filename, (@strrpos($filename, ".") ? @strrpos($filename, ".") + 1 : @strlen($filename)), @strlen($filename)));
        return ($ext == 'jpg') ? 'jpeg' : $ext;
    }


    /**
    * @return void
    * @param string $function
    * @param string $string
    * @desc Shows debugging information
    */
    function _debug($function, $string)
    {
        if ($this->_showDebug)
        {
            echo "<p><strong style=\"color:#FF0000\">Error in function $function:</strong> $string</p>\n";
        }
    }


    /**
    * @return array
    * @desc Try to ascertain what the version of GD being used is, based on phpinfo output
    */
    function _getGDVersion()
    {
        static $version = array();
       
        if (empty($version))
        {
            ob_start();
            phpinfo();
            $buffer = ob_get_contents();
            ob_end_clean();
            if (preg_match("|<B>GD Version</B></td><TD ALIGN=\"left\">([^<]*)</td>|i", $buffer, $matches))
            {
                $version = explode('.', $matches[1]);
            }
            else if (preg_match("|GD Version </td><td class=\"v\">bundled \(([^ ]*)|i", $buffer, $matches))
            {
                $version = explode('.', $matches[1]);
            }
            else if (preg_match("|GD Version </td><td class=\"v\">([^ ]*)|i", $buffer, $matches))
            {
                $version = explode('.', $matches[1]);
            }
        }
        return $version;
    }

}


?>
0
Comment
Question by:cimmer
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 12035236
Maybe the server you are running this script has another memory_limit set. Check the memory_limit on your PC and the other that don't work maybe there are different and @ImageCreateTrueColor($nx, $ny); can not allocate enough memorylimit.
0
 

Author Comment

by:cimmer
ID: 12039818
I found the problem.  I am with a hosting company running a GD Library Version less than 2.0. I am running version 1.6 to be exact. Can I give points to my self since I answered it???  heh

GD Version 1.6 does not support Image resizing functions. They were introduced in 2.0+ so ImageCreateTrueColor wont work.
0
 

Author Comment

by:cimmer
ID: 12039827
Note to admin:  you can delete this post if you wish. I am not awarding points for it.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12039841
See: http://www.experts-exchange.com/Web/Web_Languages/PHP/help.jsp#hi70 (I answered my question myself. What do I do?), but I think that question is worth too keep
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 12046595
Closed, 500 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question