Jump to content

Resizing an image..problems


NathanLedet

Recommended Posts

Creating a plugin for FrogCMS...

 

I have MyController.php and ImageResize.php in the same plugin directory

 

so when someone submits a form with an image to upload, it submits to the process_entry function:

class MyController extends PluginController
{

   function process_entry()
   {
        $image = $_FILES['contentimg'];
        $tmp_name = $image['tmp_name'];
        $name = $_FILES['contentimg']['name'];

       //include the ImageResize class to process the image
       include_once('ImageResize.php');

      //$uploaddir is defined higher up in the plugin - it works
      image_scale($tmp_name, "$uploaddir/$name", 200, 200);
   }

}

 

That generates a fatal error:

Call to undefined function image_scale()

 

K, so ImageResize.php looks like this:

 

class ImageResize {
    function image_scale($source, $dest, $width, $height)
    {
        $info = $this->image_get_info($source);
        
        //no scaling up
        if ($width > $info['width'] && $height > $info['height'])
        {
            return false;
        }
        
        $aspect= $info['height'] / $info['width'];
        if (!$height or ($width && $aspect < $height /$width))
        {
            $width = (int)min($width, $info['width']);
            $height = (int)round($width * $aspect);
        }
        else
        {
            $height = (int)min($height, $info['height']);
            $width = (int)round($height / $aspect);
        }
        
        return $this->image_gd_resize($source, $destination, $width, $height);
    } //end function image_scale()
    
    function image_get_info($file)
    {
        if( !is_file($file))
        {
            return false;
        }
        
        $details = false;
        $data = @getimagesize($file);
        
        if (is_array($data))
        {
            $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
            $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : '';
            $details = array(
            'width' => $data[0],
            'height' => $data[1],
            'extension' => $extension,
            'mime_type' => $data['mime']
            );
        }
        return $details;
    }
    
    function image_gd_resize($source, $dest, $width, $height, $source_x = 0, $source_y = 0, $source_width = null, $source_height = null)
    {
        if (!file_exists($source))
        {
            return false;
        }
        
        $info = $this->image_get_info($source);
        if(!$info)
        {
            return false;
        }
        
        $im = $this->image_gd_open($soure, $info['extension']);
        if (!$im)
        {
            return false;
        }
        
        $source_width = is_null($source_width) ? $info['width'] : $source_width;
        $source_height = is_null($source_height) ? $info['height'] : $source_height;
        
        $res = imageCreateTrueColor($width, $height);
        imageCopyResampled($res, $im, 0, 0, $source_x, $source_y, $width, $height, $source_width, $source_height);
        $result = ImageResize::image_gd_close($res, $dest, $info['extension']);
        
        imageDestroy($res);
        imageDestroy($im);
        
        return $result;
    }
    
    function image_gd_open($file, $extension)
    {
        $extension = str_replace('jpg', 'jpeg', $extension);
        $open_func = 'imagecreatefrom'.$extension;
        if(!function_exists($open_func))
        {
            return false;
        }
        return $open_func($file);
    }
    
    function image_gd_close($res, $dest, $extension)
    {
        $extension = str_replace('jpg', 'jpeg', $extension);
        $close_func = 'image'.$extension;
        if(!function_exists($close_func))
        {
            return false;        
        }
        return $close_func($res, $dest);        
    }
}

 

 

So, apparently I'm mis-using the classes because my image_scale function is not being found.... ?

 

Thanks for the help :)

Link to comment
Share on other sites

I have no idea about classes really but looking at the PHP manual maybe you need to call the class first before you use a function that is part of it?

 

so..

 

$imgResize = new ImageResize;

 

and then... image_scale

 

or maybe....

 

$result = $imgResize->{$imageResize->image_scale}();

 

http://au2.php.net/zend-engine-2.php

 

<?
class Foo {
    public $aMemberVar = 'aMemberVar Member Variable';
    public $aFuncName = 'aMemberFunc';
    
    
    function aMemberFunc() {
        print 'Inside `aMemberFunc()`';
    }
}

$foo = new Foo;
?>

You can access member variables in an object using another variable as name:
<?
$element = 'aMemberVar';
print $foo->$element; // prints "aMemberVar Member Variable"
?>

or use functions:
<?
function getVarName()
{ return 'aMemberVar'; }

print $foo->{getVarName()}; // prints "aMemberVar Member Variable"
?>

Important Note: You must surround function name with { and } or PHP would think you are calling a member function of object "foo".

you can use a constant or literal as well:
<?
define(MY_CONSTANT, 'aMemberVar');
print $foo->{MY_CONSTANT}; // Prints "aMemberVar Member Variable"
print $foo->{'aMemberVar'}; // Prints "aMemberVar Member Variable"
?>

You can use members of other objects as well:
<?
print $foo->{$otherObj->var};
print $foo->{$otherObj->func()};
?>

You can use mathods above to access member functions as well:
<?
print $foo->{'aMemberFunc'}(); // Prints "Inside `aMemberFunc()`"
print $foo->{$foo->aFuncName}(); // Prints "Inside `aMemberFunc()`"
?>

 

 

Link to comment
Share on other sites

I'm not sure if you can include a class inside the function of a class.

 

Also, you want to start your image resize script to work on $_FILES['file']['tmp_name'] as this is where the file is stored on the server unless you've already moved it elsewhere.

 

If you have moved it elsewhere, you can happily perform any resize operations on the "tmp_name" location of the file then use imagejpeg() or whatever picture format you're using to save the new image out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.