Solved

# imagejpeg doesn't work?

Posted on 2010-08-25
407 Views
I have a situation where I'm doing some image manipulation. I want to save the created image to a file & then display it in the browser.

Two files attached, the first, called merge_test, creates the image & displays it directly in the browser. It works.

The 2nd called merge_test_save attempts to SAVE the file first, then display it in HTML. The 2nd DOES NOT work.

The file IS NOT saved in the location indicated.

What's wrong?
merge-test.txt
merge-test-save.txt
0
Question by:Richard Korts
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 6
• 4

LVL 83

Expert Comment

ID: 33525349
I don't know that it matters but all PHP function names that I know of are in all lowercase.  'imagejpeg' instead of 'ImageJPEG'.

Second, do you have 'write permission' where you're trying to save the file?
0

Author Comment

ID: 33525384
To DaveBaldwin:

Yes, I thought of both of those.

I tried the imagejpeg function all in lower case, same result. Another expert had sent me that syntax & it worked OK if NOT saving the file.

I thought of the permissions issue, the folder referenced has 777 permissions.

So I don't think it's either of those.
0

LVL 83

Expert Comment

ID: 33525646
Ok, I can get the code to work (without the database stuff) if I Don't use your functions but directly open the JPG.  I get five separate errors if I try to use your functions.

Warning: imagesx(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgjpg\merge-test-save.php on line 104

Warning: imagesy(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgjpg\merge-test-save.php on line 104

Warning: imagealphablending(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgjpg\merge-test-save.php on line 87

Warning: imagecopy(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgjpg\merge-test-save.php on line 89

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgjpg\merge-test-save.php on line 109
<?php
// Image mamagement functions

// A FUNCTION TO DETERMINE IF GD IS AT LEVEL 2 OR MORE
function get_gd_info($display=FALSE) { // IS GD INSTALLED AT ALL? if (!function_exists("gd_info")) { if ($display) echo "<br/>GD NOT INSTALLED\n";
return FALSE;
}

// IF GD IS INSTALLED GET DETAILS
$gd = gd_info(); // IF DISPLAY IS REQUESTED, PRINT DETAILS if ($display)
{
echo "<br/>GD DETAILS:\n";
foreach ($gd as$key => $value) { if ($value === TRUE)  $value = 'YES'; if ($value === FALSE) $value = 'NO'; echo "<br/>$key = $value \n"; } } // RETURN THE VERSION NUMBER$gd_version = preg_replace('/[^0-9\.]/', '', $gd["GD Version"]); return$gd_version;
}

// A FUNCTION TO MAKE AN IMAGE INTO THE RIGHT WIDTH FOR PAGE DISPLAY
// WILL WORK IF GD2 NOT INSTALLED, BUT WILL MAKE BETTER IMAGES WITH GD2
// INPUT IS THE IMAGE FILE NAME, OUTPUT IS AN IMAGE RESOURCE, OR FALSE IF NO RESIZE NEEDED
function create_right_size_image($image,$width=300)
{
// IS GD HERE?
$gdv = get_gd_info(); if (!$gdv) return FALSE;

// GET AN IMAGE THING
$source = imagecreatefromjpeg("$image");

// GET THE X AND Y DIMENSIONS
$imageX = imagesx($source);
$imageY = imagesy($source);

// IF NO RESIZING IS NEEDED
if ($imageX <=$width)
{
return FALSE;
}

// THE WIDTH IS TOO GREAT - MUST RESIZE
$tnailX =$width;
$tnailY = (int) (($tnailX * $imageY) /$imageX );

// WHICH FUNCTIONS CAN RESIZE / RESAMPLE THE IMAGE?
if ($gdv >= 2) { // IF GD IS AT LEVEL 2 OR ABOVE$target = imagecreatetruecolor($tnailX,$tnailY);
imagecopyresampled ($target,$source, 0, 0, 0, 0, $tnailX,$tnailY, $imageX,$imageY);
}
else
{
// IF GD IS AT A LOWER REVISION LEVEL
$target = imagecreate($tnailX, $tnailY); imagecopyresized ($target, $source, 0, 0, 0, 0,$tnailX, $tnailY,$imageX, $imageY); } return$target;
}

// A FUNCTION TO ADD THE BORDER (SIMILAR TO WATERMARK)
// SEE http://us3.php.net/manual/en/function.imagealphablending.php#77085
function add_border (&$dst_image,$src_image, $dst_w,$dst_h, $src_w,$src_h)
{
ImageAlphaBlending($dst_image,TRUE); ImageAlphaBlending($src_image,TRUE);
ImageCopy($dst_image,$src_image, ($dst_w-$src_w), ($dst_h-$src_h), 0, 0, $src_w,$src_h);
}

?>
<?php
// format them so they can be displayed on the page
$original = 'flowercrop.jpg'; //echo "photo file = " .$original . "<br>";
$border = 'img_border_236.gif'; // READ THE IMAGE BORDER$bd = ImageCreateFromGIF($border); // READ AND RESIZE THE IMAGE //$im = create_right_size_image($original, 236);$im = imagecreatefromjpeg("$original"); // ADD THE BORDER add_border($im, $bd, imagesx($im), imagesy($im), imagesx($bd), imagesy($bd)); // save image$pfile = "nuflwr.jpg";
//echo "pfile = " . $pfile . "<br>"; // SHOW THE IMAGE ImageJPEG($im, $pfile); ?> <html> <head> <title>Image Merge Test Save</title> </head> <body> <h1>Image Merge Test Save</h1> <img alt="flowercrop (10K)" src="flowercrop.jpg" height="173" width="230" /> <img src="<?php echo$pfile; ?>">

</body>
</html>

0

LVL 83

Expert Comment

ID: 33525674
I also put '<?php' for the opening tag for the functions (wouldn't work with a short tag '<?' on my computer) and moved them to the beginning.  Most languages require the functions to be in the code before they are called.  I think it's a good practice.
0

Author Comment

ID: 33526061
To DaveBaldwin,

Yes, those are all considerations. But, the ONLY difference between the one that works & the one that DOES NOT work is this code:

Works:

ImageJPEG($im); Does not work: ImageJPEG($im, $pfile); ?> <html> <head> <title>Merge Test</title> </head> <body> <img src="<?$pfile; ?>">

</body>
</html>
<?

Everything else is EXACTLY the same. placement of the functions, etc.

So the use of imagejpeg to try to write (save) the image as a file DOES NOT work.

FYI, I have been using <? without the <?php for several years, I have NEVER had any issues with that.
0

LVL 83

Expert Comment

ID: 33526167
'<?' is short open tag,  <?php is long open tag, some servers are set up with 'short_open_tag' off like mine is.  About 2/3 of the hosting I have for customers web site has it off.

ImageJPEG($im,$pfile); does work on my computer with opening the jpg directly.
0

Author Comment

ID: 33526388
To DaveBaldwin:

Does it save the file in the path you specify so the file is there AFTER the fact?

There is much more code I'm not revealing because it's complicated & just clouds the real issue.

I have been using <? (short open tag) on THIS server for ALL my other scripts,, they all work fine.
0

LVL 83

Accepted Solution

Dave Baldwin earned 500 total points
ID: 33526496
Yes, it saves it and displays it in the <img> tag afterwards.  If you take the code I posted above and put your own fixed images in there, you can see that it works.  Then uncomment line 101 and comment line 102.  That's when it stops working for me.

One other thing to look at is $pfile = "http://www.motorvatorads.com/staging/temp_images/" .$rowph[2];  I don't think you can do a 'write' that way because you can't get 'PUT' privileges on the web server with a full URL like that.  If you can use a 'relative' url and write the file in the directory the script is in, that might work.  $pfile = "staging/temp_images/" .$rowph[2]; might also work since it doesn't have the 'http' in front of it.  'http' makes it a different 'method'.

"<? (short open tag)"  is fine as long as it is working for you.  I have several sites that I maintain that are written that way.  But now you know if you change servers and your PHP stops working.  "<? (short open tag)" is not compatible with the XML tag because it starts off the same: <? xml".  That was the reason for the change.
0

Author Comment

ID: 33527150
To DaveBaldwin:

Well, you must have been right about "http://www.motorvatorads.com/staging/temp_images/". I changed that to a relative path & it works.

Thanks for your help & persistence.
0

LVL 83

Expert Comment

ID: 33527186
Good and you're welcome.  Sorry I didn't notice that first.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
###### Suggested Courses
Course of the Month6 days, 5 hours left to enroll

#### 705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.