简体   繁体   English

上载到服务器之前裁剪图像

[英]Cropping image before of after upload to server

So I've got this PHP script to scale and crop into a square from the center; 所以我有了这个PHP脚本,可以从中心缩放并裁剪为一个正方形。

<?PHP
//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");p_image(100, 100, "test.jpg", "test.jpg");
?>

You can simply call the following function: 您可以简单地调用以下函数:

resize_crop_image(100, 100, "test.jpg", "test.jpg");p_image(100, 100, "test.jpg", "test.jpg");

Added to JSFiddle is my HTML5/JQuery preview file before uploading to the server. 我的HTML5 / JQuery预览文件已添加到JSFiddle,然后上传到服务器。

1). 1)。 Do I need to upload the image to the server before running this script? 运行此脚本之前,我需要将图像上传到服务器吗?

2). 2)。 If needing to prior upload, how can I use my form to upload into a temp location, do the job and move to a specific dir and delete the temp dir? 如果需要事先上传,我该如何使用表单将其上传到临时位置,执行作业并移至特定目录并删除临时目录?

1) Yes, a copy of the image will need to be on server before you can edit/crop it. 1)是,必须先在服务器上保存图像的副本,然后才能对其进行编辑/裁剪。 2) Uploaded files are automatically stored in the temp directory (they are usually copied out to use the image, but don't have to be). 2)上载的文件会自动存储在temp目录中(通常会复制出这些文件以使用该映像,但不必如此)。 They can be read as an image from the temp directory by your code and PHP will also automatically clean up the file at the end of the script. 您的代码可以将它们从temp目录中读取为图像,PHP还将在脚本末尾自动清理文件。

When a file gets uploaded, in the $_FILES array there is a key with the tmp name. 上传文件后,在$_FILES数组中有一个带有tmp名称的密钥。 You can just pass that into your function as the source file and it should work with imagecopyresampled with no issue. 您可以将其作为源文件传递到函数中,并且应该可以对imagecopyresampled进行正常工作。 The temp name will be something like $_FILES['nameFromFileFieldOnForm']['tmp_name'] . 临时名称将类似于$_FILES['nameFromFileFieldOnForm']['tmp_name']

if your source file or destination file is 如果您的源文件或目标文件是

$source_file = www.example.com/storage/packages/image.jpg

change it to 更改为

$source_file = ./storage/packages/image.jpg

It works for me in laravel 5 它在Laravel 5中对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM