Jump to content

Problem with with Image Resizing Code


svgmx5

Recommended Posts

I've been working on this site that allows users to upload images to the server...

 

the code i have right now basically uploads the file to the server and then resizes it and makes a new copy of it...

 

Upuntil now it was working flawlessly, however now i'm getting this error:


Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /home/content/t/h/e/thereferralpro/html/beta/region.php on line 144

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'images/projects/porncheck_star.png' is not a valid JPEG file in /home/content/t/h/e/thereferralpro/html/beta/region.php on line 144

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/content/t/h/e/thereferralpro/html/beta/region.php on line 162

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/content/t/h/e/thereferralpro/html/beta/region.php on line 165

 

In order for you guys to understand the error here's the code i'm using


//move file to the folder before resizeing
									move_uploaded_file($_FILES['image']['tmp_name'], $target);

									//resize the image and add to server
									$source_pic = $target;
									$final_pic = $dir.$final_img_name;
									$max_width = 800;
									$max_height = 600;

									$src = imagecreatefromjpeg($source_pic);
									list($width,$height)=getimagesize($source_pic);

									$x_ratio = $max_width / $width;
									$y_ratio = $max_height / $height;

									if( ($width <= $max_width) && ($height <= $max_height) ){
										$tn_width = $width;
										$tn_height = $height;
									}elseif (($x_ratio * $height) < $max_height){
										$tn_height = ceil($x_ratio * $height);
										$tn_width = $max_width;
									}else{
										$tn_width = ceil($y_ratio * $width);
										$tn_height = $max_height;
									}

									$tmp=imagecreatetruecolor($tn_width,$tn_height);
									imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

									imagejpeg($tmp,$final_pic,80);
									imagedestroy($src);
									imagedestroy($tmp);

									list($width, $height, $type, $attr) = getimagesize($final_pic);

 

Keep in mind that this code was working perfectly and now for some reason its not working anymore, and no i have not touch it at all nor i have done nay edits to this code

 

Hope someone can help me out because i have no idea what this could be.

 

Thanks

Link to comment
Share on other sites

It's probably more that the new server has error reporting disabled.

 

It's more likely in your code itself because you can be checking what the mime should be...then of those results then do a case for any imagecreatefrom, then also a case for the output of that exact mime type as well.

 

Look here , you are trying to create from a jpeg image when actually it's a png image.

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'images/projects/porncheck_star.png' is not a valid JPEG file in /home/content/t/h/e/thereferralpro/html/beta/region.php on line 144

 

Link to comment
Share on other sites

Actually i encountered a similar problem awhile back, the problem is more on the image app they are using to save the image.

I found some image apps did not work correctly with the gd functions while others did.

 

An option u may have open, is to use imagemagick or netpbm to do the conversions for u, if you have these packages available to your server as well as system function in your php configuration.

 

the other option is switch image app and testing which apps work with gd correctly. from there its just a matter of resaving the image with the ones u found to work.

Link to comment
Share on other sites

You need to check them like this and then use the proper imagecreatefrom.

 

$ext = substr($image_source, strripos($image_source, '.'),strlen($image_source));

    if($ext=='.jpg' || $ext=='.jpeg'){
        $image = imagecreatefromjpeg($image_source);
        }
    if($ext=='.gif'){
        $image = imagecreatefromgif($image_source);
        }
    if($ext=='.png'){
        $image = imagecreatefrompng($image_source);
        }
if ($ext != ".jpg" || $ext != ".jpeg" ||  $ext != ".gif" || $ext != ".png") {
die("<h2>$ext not allowed</h2><br />
<a href=\"javascript: history.go(-1)\">Click here to try again</a>");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.