0

Note: Please have a good grasp of the question and my efforts before you are tempted for any negative reaction. If there is a mistake in my approach anywhere gie me a chance to correct it.

I have a little php script on a site running on wordpress. It basically resizes the uploaded image and force offers the resized image as download prompt. Please note that I do not have any download link on the concerned page. The script works well, resizes and creates image and all, except for the part that when the downloaded image is opened in a general windows software like windows photo viewer it says "Windows photo viewer cannot open this picture, as it doesn't support this file format", but when I open it in Photoshop it displays really well. I have opened the downloaded image also in Firefox browser 24.0, wherein it says "the image file /path/to/file/" cannot be displayed because it contains errors.

Here is relevant php snippet in order to know what is happening. If needed I will write the whole code. It will be great if you can point out the mistake in the code while I am doing my own research efforts.

// create a new image resource to be utilized by imagejpeg    
$new = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

         $filename = uniqid();
         $file = 'uploads/' .$filename. '.jpeg';    
         imagejpeg($new, $file, 100);
         $size = filesize($file); 

         header("Content-Type: application/force-download; name=\"" . basename($file). "\""); 
         header("Content-Transfer-Encoding: binary"); 
         header("Content-Length: $size"); 
         header("Content-Disposition: attachment; filename=\"" . basename($file) . "\""); 
         header("Expires: 0"); 
         header("Cache-Control: no-cache, must-revalidate"); 
         header("Pragma: no-cache"); 
         readfile($file);

EFFORTS UPDATES :

First update : I read this thread and based on it's suggestions tried to add ob_end_clean() before the readfile($file); and also comment out the $size, still getting corrupt images.

Second Update : I have also tried the code given below, as was suggested in the solution here. Apart from this, please keep in mind that I have also tried minor corrections after having read other numerous threads here, and i am still looking.

header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

This last effort makes me think that maybe something is wrong with the way I am assigning the image-extension in my original code. Currently I am assigning it as $file = 'uploads/' .$filename. '.jpeg'; which could be wrong. If I am how should I correct it ?

Community
  • 1
  • 1
gurung
  • 628
  • 2
  • 11
  • 33
  • It could be a problem with the Content-Type header. Should be `image/jpeg` for a jpg picture. Propably this solves your issue. And try `ob_end_clean()` instead of `ob_clean()` and don't call `flush()` afterwards. – TiMESPLiNTER Oct 31 '13 at 07:42
  • I know if an image is in `cmyk` those apps wont display it. – gwillie Oct 31 '13 at 07:42
  • did you tried removing `print_r($file);` from your code? – bystwn22 Oct 31 '13 at 07:44
  • @gurung just found out `GD` doesn't support color management so it will fail to load `CMYK` images. Your issue is elsewhere – gwillie Oct 31 '13 at 08:01
  • @gurung Have you tried to send an image which displays correct in windows photo view to test if the resizing is the problem or the sending of the image to the client? – TiMESPLiNTER Oct 31 '13 at 08:05
  • @TiMESPLiNTER I am not sure about what you are suggesting. But the jpeg file that is created on the server is flawless. It is just that what I download is corrupted so there must be something wrong with the download headers. – gurung Oct 31 '13 at 08:09
  • I suggested to deliver an image which is for sure not corrupted. But as you say now you know that your resized image displays everywhere correct as long as it is not sent via php to the client. Can you post the whole script content? From beginning propably there is somewhere at the end a space or something like this? – TiMESPLiNTER Oct 31 '13 at 08:12
  • @TiMESPLiNTER Solved. there was some tiny but unnecessary html in the script which caused the issue, interesting !! One last question, if I keep the `ob_end_clean();` do I need to to put `ob_start();` somewhere above `imagejpeg`, then i am getting rid of `flush();`. Also put this as answer please, so someone could save time in future. thanks a lot. – gurung Oct 31 '13 at 08:26

1 Answers1

0

Just in case someone is not able to find the solution buried within comments. Here is how I solved the issue. Just get rid of all and any kind of html form the php page which contains the download code.

gurung
  • 628
  • 2
  • 11
  • 33