Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

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
0
Richard Korts
Asked:
Richard Korts
  • 6
  • 4
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Richard KortsAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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>

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Dave BaldwinFixer of ProblemsCommented:
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
 
Richard KortsAuthor Commented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
'<?' 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
 
Richard KortsAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Richard KortsAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
Good and you're welcome.  Sorry I didn't notice that first.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now