Function call with dollar sign in front

I am new to php and I found a tutorial about cropping an image with weird instruction that I have never seen. I don't know how to find it.

$src_img = $image_create($source_file);

      

Here is the complete tutorial code

 //resize and crop image by center
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];

    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            break;

        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 7;
            break;

        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $quality = 80;
            break;

        default:
            return false;
            break;
    }

    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $src_img = $image_create($source_file);

    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
    }else{
        //cut point by width
        $w_point = (($width - $width_new) / 2);
        imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
    }

    $image($dst_img, $dst_dir, $quality);

    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);
}
//usage example
resize_crop_image(100, 100, "test.jpg", "test.jpg");

      

+3


source to share


3 answers


$image_create

returns a string.

This sting is a dynamic function (whose name is determined by the runtime)

Link:



http://php.net/manual/en/functions.variable-functions.php

Instead of adding 3 if operators to select three options:   imagecreatefromgif()

, imagecreatefrompng()

and imagecreatefromjpeg()

is executed variable that will toggle the function variable (name) and that will be used.

Which is easier to use.

+4


source


In the beginning: I don't know which tutorial you are following, but it doesn't look particularly good. The code looks very messy and a little outdated in my opinion. It also doesn't follow coding standards at all ... I would get clear.

To answer your question:

$image_create = 'imagecreatefromjpeg';

      

Assigns a string to a variable at first glance, but that string is also a function name . Basically, read the following:



$src_img = $image_create($source_file);

      

as one of three calls:

$src_img = imagecreatefromjpeg($source_file);
//or
$src_img = imagecreatefrompng($source_file);
//or
$src_img = imagecreatefromgif($source_file);

      

Depending on the value $image_create

...

+5


source


It calls the function through a variable containing the name of the function.

Note that this is $image_create

set depending on what type of image the code is trying to generate. This allows the rest of the code to not care about the image type being consolidated, which is good practice.

+2


source







All Articles