daveyp2004 Posted March 19, 2012 Share Posted March 19, 2012 Hi everyone, would be grateful if anyone can help me. I recently changed my hosting package and didnt realize that the new package uses PHP 5. There is no option to use an earlier version but my thumbnail script that worked on PHP 4 just shows a red cross where the thumbnail should be. Can anyone tell me why the script below would not work with PHP 5. I have a basic understanding of how scripts work but I am not an expert so if you could make any replies easy to understand I have checked and the new server does have GD library version 2.0.34 installed - not sure if there is anything else I need to check ? script called: uktn.php <? $maxwidth = 200; $maxheight = 200; $image_path = $_GET['im']; $ext = explode('.', $image_path); $i = count($ext)-1; if ($ext[$i] == 'jpg' || $ext[$i] == 'jpeg' || $ext[$i] == 'JPG' || $ext[$i] == 'JPEG' ) { $img = @imagecreatefromjpeg($image_path); } else if ($ext[$i] == 'png' || $ext[$i] == 'PNG') { $img = @imagecreatefrompng($image_path); } else if ($ext[$i] == 'gif' || $ext[$i] == 'GIF') { $img = @imagecreatefromgif($image_path); } if ($img) { $width = imagesx($img); $height = imagesy($img); $scale = min($maxwidth/$width, $maxheight/$height); if ($scale < 1) { $new_width = floor($scale*$width); $new_height = floor($scale*$height); $tmp_img = imagecreatetruecolor($new_width, $new_height); imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagedestroy($img); $img = $tmp_img; } } if (!$img) { exit ("You must specify an image!"); } header("Content-type: image/png"); imagepng($img,'',100); ?> Many thanks, Dave Quote Link to comment https://forums.phpfreaks.com/topic/259275-thumbnail-script-stopped-working-after-upgrade-to-php-version-5/ Share on other sites More sharing options...
DavidAM Posted March 19, 2012 Share Posted March 19, 2012 1) Don't use short open tags, some servers have them turned off (which is the default in 5.3, I think) and the code will not execute. So, instead of <? always use <?php 2) Turn on error reporting at the start of your script error_reporting(-1); ini_set('display_errors', 1); 3) Remove the @-sign. In fact, remove it from your memory. This construct HIDES errors, it does NOT fix them. Once you have removed these (and turned on error reporting) you may find that these statements are throwing errors. Fix these errors. --- IMHO a "Notice" is an error, a "Warning" is an error. Your logic should prevent any notices and warnings. If you come across errors that you can't figure out, post them here (along with the updated code) and we'll see what we can do. 4) It looks like this script is used as the SRC of an IMG tag. If that is the case, you will not see the errors or your programmed error message. To see your messages, request the image directly in the address bar of the browser. Note: error_reporting and display_errors reveals information about your site. These settings should be used in development ALWAYS. The should almost always be off in production. Quote Link to comment https://forums.phpfreaks.com/topic/259275-thumbnail-script-stopped-working-after-upgrade-to-php-version-5/#findComment-1329148 Share on other sites More sharing options...
daveyp2004 Posted March 20, 2012 Author Share Posted March 20, 2012 Hi David, Thanks for your detailed reply. I have made the changes you suggested but see no errors. You are correct that the script is used as the source of an image tag. The script is called uktn.php is called from my index page as follows: <td width="205" rowspan="8" class="menuShadow_lite"><a href="forms/user_uploads/<?php echo $row_jetskiRS['img']; ?>" target="_blank"><img src="uktn.php?im=forms/user_uploads/<?php echo $row_jetskiRS['img']; ?>" border="0" class="menuShadow_lite"></a><br> </td> I entered the URL manually in my browser but do not see any errors (just a red cross where thumbnail should be): http://www.jetski.ukwebad.com/uktn.php?im=forms/user_uploads/68f00fc57c0fc71030fb53cd44f67d28.jpg As an experiment I added the full URL path of the image: http://www.jetski.ukwebad.com/uktn.php?im=http://www.jetski.ukwebad.com/forms/user_uploads/68f00fc57c0fc71030fb53cd44f67d28.jpg and I then saw the following errors: Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: URL file-access is disabled in the server configuration in /homepages/42/d82737056/htdocs/jetskiukwebad/uktn.php on line 11 Warning: imagecreatefromjpeg(http://www.jetski.ukwebad.com/forms/user_uploads/68f00fc57c0fc71030fb53cd44f67d28.jpg) [function.imagecreatefromjpeg]: failed to open stream: no suitable wrapper could be found in /homepages/42/d82737056/htdocs/jetskiukwebad/uktn.php on line 11 You must specify an image! So it looks like error reporting is working and that the original script is running without generating any errors but it is not showing the thumbnail ? Any other suggestions for what might be wrong ? The script did work before upgrade to server using php 5. Many thanks, Dave Quote Link to comment https://forums.phpfreaks.com/topic/259275-thumbnail-script-stopped-working-after-upgrade-to-php-version-5/#findComment-1329342 Share on other sites More sharing options...
DavidAM Posted March 20, 2012 Share Posted March 20, 2012 Interesting. If I request the script without the im parameter -- http://www.jetski.ukwebad.com/uktn.php -- I get the following Notice: Undefined index: im in /homepages/42/d82737056/htdocs/jetskiukwebad/uktn.php on line 6 Notice: Undefined variable: img in /homepages/42/d82737056/htdocs/jetskiukwebad/uktn.php on line 17 Notice: Undefined variable: img in /homepages/42/d82737056/htdocs/jetskiukwebad/uktn.php on line 31 You must specify an image! However, if I add the im parameter -- http://www.jetski.ukwebad.com/uktn.php?im=forms/user_uploads/68f00fc57c0fc71030fb53cd44f67d28.jpg -- I get an image which is a line of text that says: The image"http://www.jetski.ukwebad.com/uktn.php?im=forms/user_uploads/68f00fc57c0fc71030fb53cd44f67d28.jpg" cannot be displayed because it contains errors It is actually an image of the text and not an html document. That is strange. I've never seen it before. On to the problem. The only thing I can see is the call to imagepng. The third parameter is called "quality", but seems to actually be compression level. The valid range is 0 - 9. Try changing that line to: imagepng($img, null, 9); Note that the higher compression levels take longer to compress but produce smaller files for download. Quote Link to comment https://forums.phpfreaks.com/topic/259275-thumbnail-script-stopped-working-after-upgrade-to-php-version-5/#findComment-1329410 Share on other sites More sharing options...
daveyp2004 Posted March 20, 2012 Author Share Posted March 20, 2012 Hi David, You are a genius ! Changing the parameters for imagepng() as suggested fixed the problem. Many thanks for your help, I dont think I would ever have found that ! Cheers, Dave Quote Link to comment https://forums.phpfreaks.com/topic/259275-thumbnail-script-stopped-working-after-upgrade-to-php-version-5/#findComment-1329421 Share on other sites More sharing options...
DavidAM Posted March 20, 2012 Share Posted March 20, 2012 Glad to help. Make sure you turn off error reporting on your production server. Mark the topic Solved Have fun! Quote Link to comment https://forums.phpfreaks.com/topic/259275-thumbnail-script-stopped-working-after-upgrade-to-php-version-5/#findComment-1329438 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.