Richard Korts
asked on
imagejpeg doesn't work?
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
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
ASKER
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.
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.
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\imgj pg\merge-t est-save.p hp on line 104
Warning: imagesy(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj pg\merge-t est-save.p hp on line 104
Warning: imagealphablending(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj pg\merge-t est-save.p hp on line 87
Warning: imagecopy(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj pg\merge-t est-save.p hp on line 89
Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj pg\merge-t est-save.p hp on line 109
Warning: imagesx(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj
Warning: imagesy(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj
Warning: imagealphablending(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj
Warning: imagecopy(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj
Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\Inetpub\wwwroot\ee\imgj
<?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>
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.
ASKER
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:
header('Content-type: image/jpg');
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.
Yes, those are all considerations. But, the ONLY difference between the one that works & the one that DOES NOT work is this code:
Works:
header('Content-type: image/jpg');
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.
'<?' 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.
ImageJPEG($im, $pfile); does work on my computer with opening the jpg directly.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
Good and you're welcome. Sorry I didn't notice that first.
Second, do you have 'write permission' where you're trying to save the file?