Search code examples
phpclassimagicksystem.drawing.imaging

How can I call a function in a php class?


This is a sample code:

sample code

I want to call it in another page:

include 'root of class file';
$r = new ImagineResizer();

I got this error:

Fatal error: Class 'ImagineResizer' not found in C:\WampDeveloper\Websites\example.com\webroot\images.php on line 13

Also call the function:

$r->resize('c:/test1.jpg', 'c:/test2.jpg');

Solution

  • As seen in your sample code the class is located in another namespace :

    <?php 
    
    namespace Acme\MyBundle\Service;
    use Symfony\Component\HttpFoundation\File\File;
    use Imagine\Image\ImagineInterface;
    use Imagine\Image\BoxInterface;
    use Imagine\Image\Point;
    use Imagine\Image\Box;
    
    class ImagineResizer {
        //-- rest of code     
    }
    

    To use a class in another namespace you need to point out where the file is : First include the class (manual or with autoloading)

    Then u can create an instance in 2 ways. First way with the use-keyword

    use Acme\MyBundle\Service\ImageResizer;
    $object = new ImageResizer();
    

    Or point to the class absolute :

    $object = new \Acme\MyBundle\Service\ImageResizer();