0

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.

  • Don't. Resize the image *once* when it's uploaded. Make and store multiple sizes. Disk space is far cheaper and easier to scale than on-demand CPU scaling. Source: I have 24TB of images. – Sammitch May 19 '16 at 18:04

2 Answers2

0

For your $image function , do not specify a destination directory (null) and an image stream will be created.

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

That should send the image to the user

John
  • 912
  • 6
  • 12
  • That was quick and easy. Thank you john <3 –  May 19 '16 at 17:43
  • Just 1 last question... the output image's name is something like download.php, the same name that my php file has. How can i return the original file name? Do i have to use another header? –  May 19 '16 at 17:59
  • Correct, another header will do the trick: Content-Disposition: attachment; filename=FILENAME – John May 19 '16 at 18:48
0

You just need to set the proper headers and echo the output. Every PHP request "downloads a file" to the browser, more or less. 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.

Community
  • 1
  • 1
Sean Halls
  • 112
  • 1
  • 11
  • That was cool man, i was just about to ask how to force download the image! Thanks a lot! –  May 19 '16 at 17:44