简体   繁体   English

使用PHP即时裁剪图像大小

[英]Crop resize images on the fly using PHP

I'm building a wallpaper website and therefore i need to be able to resize the original image before downloading. 我正在建立一个墙纸网站,因此在下载之前,我需要能够调整原始图像的大小。 I tried this code to resize the image: 我尝试使用以下代码来调整图像大小:

//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);
}

And this will do the resize: 这将调整大小:

resize_crop_image($width, $height, $image_URL, $save_URL)

This code works fine for me but i want to ONLY send the output to user's browser, since saving thousands of extra images isn't possible. 这段代码对我来说很好用,但是我只想将输出发送到用户的浏览器,因为不可能保存成千上万的额外图像。 There are libraries i can use, but i don't want to use a third party snippet. 我可以使用某些库,但我不想使用第三方代码段。 Is there a way to alter this code in the way i desire? 有没有办法以我想要的方式更改此代码? Thanks. 谢谢。

For your $image function , do not specify a destination directory (null) and an image stream will be created. 对于$ image函数,请不要指定目标目录(空),否则将创建图像流。

header("Content-type:{$mime}");

That should send the image to the user 那应该将图像发送给用户

You just need to set the proper headers and echo the output. 您只需要设置适当的标题并回显输出即可。 Every PHP request "downloads a file" to the browser, more or less. 每个PHP请求或多或少都会“下载文件”到浏览器。 You just need to specify how the browser handles it. 您只需要指定浏览器如何处理它即可。 So try something like: 因此,尝试类似:

header("Content-Type: $mime");
header("Content-Disposition: attachment; filename=$dst_img");
echo $dst_img;

That should do it for you. 那应该为你做。

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

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