在前面调用带有美元符号的功能

Ali*_*oly 3 php

我是php的新手,我找到了一个关于裁剪图像的教程,我从未看到过一个奇怪的指令.我不知道如何搜索它.

$src_img = $image_create($source_file);
Run Code Online (Sandbox Code Playgroud)

这是本教程的完整代码

 //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");
Run Code Online (Sandbox Code Playgroud)

Pup*_*pil 5

$image_create 返回一个字符串.

这个刺痛是一个动态函数(其名称取决于运行时间)

参考:

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

代替加入3 if语句用于选择三种功能: imagecreatefromgif(),imagecreatefrompng()imagecreatefromjpeg(),可变取这将切换函数的变量(名称),并且将被使用.

哪个更容易使用.


Eli*_*gem 5

首先:我不知道你正在关注什么教程,但它看起来并不是特别好.在我看来,代码看起来非常混乱,有点陈旧.它根本不遵循编码标准 ......我很清楚.

但是要回答你的问题:

$image_create = 'imagecreatefromjpeg';
Run Code Online (Sandbox Code Playgroud)

乍一看,是为变量分配一个字符串,但该字符串恰好也是一个函数名.基本上,请阅读:

$src_img = $image_create($source_file);
Run Code Online (Sandbox Code Playgroud)

作为三个电话之一:

$src_img = imagecreatefromjpeg($source_file);
//or
$src_img = imagecreatefrompng($source_file);
//or
$src_img = imagecreatefromgif($source_file);
Run Code Online (Sandbox Code Playgroud)

取决于$image_create...... 的价值