Unable to open images with imagejpeg() [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Unable to open images with imagejpeg() [Solved]

Post by Jade »

Hey there.

I'm working on a site for someone and I've run into a problem. Anytime I try to open the image to write over it I get errors. Both the folder and the image are chmod 777. I've tried looking online for solutions with no luck. I'm running out of ideas, anyone had this problem before?

Code: Select all

function resizeImage($filename)
{
//Figure out image type
$type = substr($filename, (strlen($filename)-4), strlen($filename));

//1/4 of the image size
if (getimagesize($filename) > 20000)
	$percent = 0.25;
else //3/4 of the image size
	$percent = 0.75;

// Set a maximum height and width
$width = 120;
$height = 90;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);


if ($type == ".jpg" || $type == "jpeg")//jpg image
	$image = imagecreatefromjpeg($filename);
else if ($type == ".gif") //gif inage
	$image = imagecreatefromgif($filename);
else if ($type == ".png") //png image
	$image = imagecreatefrompng($filename);
else
	return "Error!";
	
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

if ($type == ".jpg" || $type == "jpeg")//jpg image
{
	imagejpeg($image_p, "uploads/newimage.jpg", 100);
	return "<img src=\"uploads/newimage.jpg\" />";
}
else if ($type == ".gif") //gif inage
{
	imagegif($image_p, "uploads/newpicture.gif", 100);
	return "<img src=\"uploads/newpicture.gif\" />";
}
else if ($type == ".png") //png image
{
	imagepng($image_p, "uploads/newpicture.png", 100);
	return "<img src=\"uploads/newpicture.png\" />";
}
else
	return "Error!";
}
[/b]
Last edited by Jade on Thu Jan 11, 2007 5:27 pm, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

what errors do you get exactly?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://graphicart.com/uploads/newimage.jpg' for writing in /home/stockinv/public_html/graphicart/kernel/functions.php on line 818

Not that the error message helps much....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't write or call Jade, what's the deal? :)

It really says the url?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

Lol, I have no clue. No, I took the url out, thats an old error message. Too lazy to refresh the page, after all, that requires clicking...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Have you tried touching it first?

Code: Select all

if ($type == ".jpg" || $type == "jpeg")//jpg image
{
        touch("uploads/newimage.jpg");
        imagejpeg($image_p, "uploads/newimage.jpg", 100);
        return "<img src=\"uploads/newimage.jpg\" />";
}
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

Great! That worked. Kuddos to you :D
Post Reply