?
Solved

imagejpeg doesn't work?

Posted on 2010-08-25
10
Medium Priority
?
413 Views
Last Modified: 2012-05-10
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
Comment
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
10 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
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

by:Richard Korts
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

by:Dave Baldwin
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>

Open in new window

0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
LVL 83

Expert Comment

by:Dave Baldwin
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

by:Richard Korts
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:

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
 
LVL 83

Expert Comment

by:Dave Baldwin
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

by:Richard Korts
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

by:
Dave Baldwin earned 2000 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

by:Richard Korts
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

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

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

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

Join & Ask a Question