maxelct Posted January 8, 2013 Share Posted January 8, 2013 (edited) Hi I have a little php and a site that used to work (apparently, I didn't build it) has recently stopped. The problem ius that you used to be able to upload an image, and it would make three different sized versions of it and then link them to the various bits of profile information. This has recetnly stopped working, and I shoujld say the site is around 6 years old. What happens now is that when you create a new profile, select an image for upload and press "Submit" you get a whole pile of errors mostly along the lines of [b]Warning[/b]: imagecopyresampled(): supplied argument is not a valid Image resource in [b]/websites/LinuxPackage02/.../public_html/admin/php/nslib_php/nslib_image.php[/b] on line [b]133[/b] [b]Warning[/b]: imagedestroy(): supplied argument is not a valid Image resource in [b]/websites/LinuxPackage02/.../public_html/admin/php/nslib_php/nslib_image.php[/b] on line [b]136[/b] Using ftp I can see that 3 images have been created on the server, and they do link to the profiles but they are just "Black" rectangles. Here;s some of the code creating the problem (I think) if ($target_type == 'image/jpeg') { // RESIZE the image with the parameters specified imagecopyresampled($target_file, $source_file, 0, 0, $source_x, $source_y, $target_width, $target_height, $source_width, $source_height); It looks to me that whoever has built the site used a library, but I cant find that library or the builder. Would anyone out there be able to suggest what I need to do? Happy to supply more detail if it helps! Many thanks Edward Edited January 8, 2013 by KevinM1 code tag Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/ Share on other sites More sharing options...
scootstah Posted January 8, 2013 Share Posted January 8, 2013 The error is telling you that $target_file is not an Image resource. This means that, further up the code, that Image resource did not get created properly. Most likely because the image failed to upload. The library looks like the standard PHP GD library. Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404235 Share on other sites More sharing options...
maxelct Posted January 9, 2013 Author Share Posted January 9, 2013 OK - thanks that's useful. I suppose this means that somewhere dummy images are created by the code. Recently the host changed the ftp password - that's not relevant though is it? Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404393 Share on other sites More sharing options...
gizmola Posted January 9, 2013 Share Posted January 9, 2013 The errors and snippet of code just aren't enough to go on. It certainly appears that the source file is not valid so that when the routines run, you get empty/black images. Something changed probably having to do with permissions. I wouldn't suspect ftp unless the upload system involves ftp. Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404395 Share on other sites More sharing options...
maxelct Posted January 9, 2013 Author Share Posted January 9, 2013 (edited) Thanks - yes I know I haven't given you very much! I have looked through the process from the form which calls.this function: require_once 'profiles_lib.php'; : : // function: add_profile (array $form_data, array $file_data) // purpose: adds a profile to the db // parameters: [1] $form_data (array) -> data collected from the form // [2] $file_data (array) -> file data concearning the photo // returns: nothing function add_profile ($form_data, $file_data) { // format the date of birth into mysql format $form_data['dob'] = uk2mysql_date($form_data['dob']); // resize photos if one is supplied if (is_file($file_data['tmp_name'])) { // get the next image number $next_image = time(); resize_image ($file_data['tmp_name'], '../../../profile_photos/'.$next_image.'_large.jpg', $GLOBALS['config']['large_photo_width'], $GLOBALS['config']['large_photo_height'], 'centre'); resize_image ($file_data['tmp_name'], '../../../profile_photos/'.$next_image.'_medium.jpg', $GLOBALS['config']['medium_photo_width'], $GLOBALS['config']['medium_photo_height'], 'centre'); resize_image ($file_data['tmp_name'], '../../../profile_photos/'.$next_image.'_small.jpg', $GLOBALS['config']['small_photo_width'], $GLOBALS['config']['small_photo_height'], 'centre'); // and set the image number field $form_data['photo_number'] = $next_image; } // create a db object $db = new db_object; // insert the data into the db $db->simple_insert ('nscms_profiles', $form_data); } I can see here that the image is resized, given a number and put into a database object (I think). What I can't see is how the image gets onto the server. I have included profiles_lib.php - which I THINK is where an upload is happening - but I could well be wrong here! I really appreciate any help you can give me. profiles_lib.php Edited January 9, 2013 by maxelct Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404398 Share on other sites More sharing options...
PFMaBiSmAd Posted January 9, 2013 Share Posted January 9, 2013 You are approaching this problem from the tail-ass end and trying to get someone who hasn't even seen that part of the animal to tell you the eye color of the animal it is attached to. Is this an open-source 3rd party script that someone can just download the whole thing from the author? Snippets of it where errors are being reported at aren't where the problem is occurring at. Also, from the code you have posted, it doesn't have any/good error checking logic to prevent follow-on errors when something didn't occur perfectly in a previous step. Just because if(is_file($file_data['tmp_name'])){ is true, doesn't mean that the upload worked. One of the possibilities is that the upload is failing. What does adding the following, right before the calls to the resize_image() function, show - echo '<pre>',print_r($file_data,true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404418 Share on other sites More sharing options...
maxelct Posted January 9, 2013 Author Share Posted January 9, 2013 Good comments! Its a bespoke system that appears to have been developed (perhaps with libraries) about 6 or 7 years ago. I added in the error line and it showed this; Array ( [name] => 14032010216.jpg [type] => image/jpeg [tmp_name] => /tmp/phpC1jCtW [error] => 0 [size] => 65469 ) Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404424 Share on other sites More sharing options...
PFMaBiSmAd Posted January 9, 2013 Share Posted January 9, 2013 That would indicate that the upload worked (zero error element and a specific file size.) The problem is occurring in something the resize_image() function is doing or in the parameter values being passed into that function. Since black output images are being produced (I'll assume that they were at least in the correct destination folder and of the correct three different sizes as specified by the $GLOBALS['config'] values), what the resize_image() function code is doing, would be where to look next. Where there any other php errors being reported in the code corresponding to the resize_image() function? Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404427 Share on other sites More sharing options...
maxelct Posted January 9, 2013 Author Share Posted January 9, 2013 (edited) Thanks - yes, three black images in the correct folder and with the correct names are created. Here are alll the error messages: Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /websites/LinuxPackage02/...admin/php/nslib_php/nslib_image.php on line 133 Warning: imagedestroy(): supplied argument is not a valid Image resource in /websites/LinuxPackage02/.../admin/php/nslib_php/nslib_image.php on line 136 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /websites/LinuxPackage02/...admin/php/nslib_php/nslib_image.php on line 133 Warning: imagedestroy(): supplied argument is not a valid Image resource in /websites/LinuxPackage02/.../admin/php/nslib_php/nslib_image.php on line 136 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /websites/LinuxPackage02/.../admin/php/nslib_php/nslib_image.php on line 133 Warning: imagedestroy(): supplied argument is not a valid Image resource in /websites/LinuxPackage02/.../admin/php/nslib_php/nslib_image.php on line 136 Edited January 9, 2013 by maxelct Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404435 Share on other sites More sharing options...
maxelct Posted January 9, 2013 Author Share Posted January 9, 2013 Hi - just got a "Help" message from the hosting company. They say nothing has changed on their servers and suggested: Please can you try adding this line to your scripts 'configuration'file and retest: putenv("MAGICK_THREAD_LIMIT=1"); Sadly didn't make any difference though... Just thought I'd add it to the mix Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404508 Share on other sites More sharing options...
PFMaBiSmAd Posted January 9, 2013 Share Posted January 9, 2013 ... The problem is occurring in something the resize_image() function is doing ... ..., what the resize_image() function code is doing, would be where to look next. Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404516 Share on other sites More sharing options...
maxelct Posted January 10, 2013 Author Share Posted January 10, 2013 Thanks again. I have found the resize_imgae function and have started to see if the varoius functions within it are working. Here's the output create [b] $target_type=image/jpeg[/b] [b] imagecreatetruecolor was true[/b] What I think might be odd (!) is that source_type test and the imagecreatefromjpeg doesn't seem to generate true of false. Or am I missing the point? // function: resize_image (string $source_image, string $target_image, int $target_width, int $target_height[, boolean $crop_position]) : : ///////////////////////////////////////// added a test here?????????????? // if the target filetype is a jpeg, create a truecolour image if ($target_type == 'image/jpeg') { //edward echo '[b] $target_type='.$target_type.'[/b]'; // create an empty image in memory with the target's dimensions //$target_file = imagecreatetruecolor($target_width, $target_height); if($target_file = imagecreatetruecolor($target_width, $target_height)){ echo '[b]imagecreatetruecolor was true[/b]'; }else{ echo '[b] imagecreatetruecolor was false[/b]'; }; // if the source image is a jpeg then load it ///////////////////////////////////////// is this odd?????????????? if ($source_type == 'image/jpeg') { echo ' [b] source_type='.$source_type.'[/b] '; // load the source image as a jpeg from disk into memory // edward $source_file = imagecreatefromjpeg($source_image); if($source_file = imagecreatefromjpeg($source_image)){ echo '[b] imagecreatefromjpeg was true[/b]'; }else{ echo '[b] imagecreatefromjpeg was false[/b]'; }; } elseif ($source_type == 'image/gif') { // otherwise load the source image as a gif from disk into memory $source_file = imagecreatefromgif($source_image); } : : : } Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404646 Share on other sites More sharing options...
maxelct Posted January 11, 2013 Author Share Posted January 11, 2013 Hi - still really need help on this - please Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404967 Share on other sites More sharing options...
PFMaBiSmAd Posted January 11, 2013 Share Posted January 11, 2013 What I think might be odd (!) is that source_type test and the imagecreatefromjpeg doesn't seem to generate true of false. ^^^ What have you done to debug why the code isn't doing what you expect for the source image data? I'm also not sure why you didn't just posted the complete code for that function so that someone could directly point you in the right direction to look. Out of context snippets of code are like pieces to a jig-saw puzzle and you are asking us to tell you what the picture on the box is based on having two or three pieces of the puzzle. Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404971 Share on other sites More sharing options...
maxelct Posted January 11, 2013 Author Share Posted January 11, 2013 The function is quite long (to my mind) and I didn't want to annoy anyone. Sorry. Fact is I didn't know what to do after this point - what tests to try and run. As you no doubt can tell I am on the edge (and way over) what I really know. // function: resize_image (string $source_image, string $target_image, int $target_width, int $target_height[, boolean $crop_position]) // purpose: resizes an image using the parameters supplied // parameters: [1] $source_image (string) -> the filename and path of the source file // [2] $target_image (string) -> the filename and path of the target file // [3] $target_width (int) -> the width of the target file (set as false for this to be calculted by the height) // [4] $target_height (int) -> the height of the target file (set as false for this to be calculted by the width) // [5] $crop_position (string) -> (optional) if the image is to be centre cropped (square), values are: left, right, top, bottom, centre // [6] $quality (int) -> (optional) the jpeg quality // returns: whether the image creation succeeded or failed function resize_image ($source_image, $target_image, $target_width, $target_height, $crop_position = false, $quality = 75) { // set the memory limit higher and the execution time higher for the script as large // attachments may be processed ini_set('memory_limit', '32M'); ini_set('max_execution_time', '60'); $return_var = false; // initialise the return variable // get the source file type $source_type = get_mime_type($source_image); // get the target file type if (is_numeric(strpos(strtolower($target_image), 'jpg')) || is_numeric(strpos(strtolower($target_image), 'jpeg'))) { // if the type is jpeg $target_type = 'image/jpeg'; } elseif (is_numeric(strpos(strtolower($target_image), 'gif'))) { // if the type id gif $target_type = 'image/gif'; } // get the dimensions of the source image and check whether the file is valid if (list($source_width, $source_height) = getimagesize($source_image)) { // calculate the dimensions of the new photo // if there is no width set then calculate from the ratio of the source image and // the height dimension if (!$target_width) { $target_width = ($source_width / $source_height ) * $target_height; } // if there is no height set then calculate from the ratio of the source image and // the width dimension if (!$target_height) { $target_height = ($source_height / $source_width ) * $target_width; } // if the target filetype is a jpeg, create a truecolour image if ($target_type == 'image/jpeg') { //edward echo ' [b] $target_type='.$target_type.'[/b]'; // create an empty image in memory with the target's dimensions //$target_file = imagecreatetruecolor($target_width, $target_height); if($target_file = imagecreatetruecolor($target_width, $target_height)){ echo '[b] imagecreatetruecolor was true[/b]'; }else{ echo '[b] imagecreatetruecolor was false[/b]'; }; } else { // otherwise create a pallete based image // create an empty image in memory with the target's dimensions $target_file = imagecreate($target_width, $target_height); } // if the source image is a jpeg then load it if ($source_type == 'image/jpeg') { echo ' [b] source_type='.$source_type.'[/b]'; // load the source image as a jpeg from disk into memory // edward $source_file = imagecreatefromjpeg($source_image); if($source_file = imagecreatefromjpeg($source_image)){ echo '[b] imagecreatefromjpeg was true[/b]; }else{ echo '[b] imagecreatefromjpeg was false[/b]'; }; } elseif ($source_type == 'image/gif') { // otherwise load the source image as a gif from disk into memory $source_file = imagecreatefromgif($source_image); } // preset the source x and y positions of the output image $source_x = 0; $source_y = 0; // copy the source width and height to some other variables used in calculation $calc_width = $source_width; $calc_height = $source_height; // if it's specified that the image should be cropped if ($crop_position) { // calculate the source & target ratios $source_ratio = $source_width / $source_height; $target_ratio = $target_width / $target_height; // calculate the other dimension if ($source_ratio > $target_ratio) { $source_width = ($target_width / $target_height ) * $calc_height; } else { $source_height = ($target_height / $target_width ) * $calc_width; } // depending on which crop position is selected, the calculations will be // done to extract the required part switch ($crop_position) { case 'top': $source_x = ($calc_width - $source_width) / 2; $source_y = 0; break; case 'bottom': $source_x = ($calc_width - $source_width) / 2; $source_y = $calc_height - $source_height; break; case 'left': $source_x = 0; $source_y = ($calc_height - $source_height) / 2; break; case 'right': $source_x = $calc_width - $source_width; $source_y = ($calc_height - $source_height) / 2; break; default: $source_x = ($calc_width - $source_width) / 2; $source_y = ($calc_height - $source_height) / 2; break; } } // if a jpeg is to be created if ($target_type == 'image/jpeg') { // RESIZE the image with the parameters specified imagecopyresampled($target_file, $source_file, 0, 0, $source_x, $source_y, $target_width, $target_height, $source_width, $source_height); // remove the image of the source file from memory imagedestroy($source_file); // create a jpeg file on disk from the newly created image in memory and write to disk $return_var = imagejpeg($target_file,$target_image, $quality); // remove the image of the target file from memory imagedestroy($target_file); // if a gif file is to be created } elseif ($target_type == 'image/gif') { // RESAMPLE the image with the parameters specified imagecopyresized($target_file, $source_file, 0, 0, $source_x, $source_y, $target_width, $target_height, $source_width, $source_height); // remove the image of the source file from memory imagedestroy($source_file); // create a gif file on disk from the newly created image in memory and write to disk $return_var = imagegif($target_file,$target_image); // remove the image of the target file from memory imagedestroy($target_file); } } return $return_var; // return the status of the function } Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404974 Share on other sites More sharing options...
mikosiko Posted January 11, 2013 Share Posted January 11, 2013 well... you have the resize_image() function.... have you tried to test it alone with an existent image that you have?... <?php function resize_image(......... parameters.......) {\ /// code } // call the function here resize_image(... parameters...); ?> and test if it produce an image? Quote Link to comment https://forums.phpfreaks.com/topic/272853-images-used-to-upload-suddenly-they-dont/#findComment-1404983 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.