trouble with jpeg output

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
bluesguitarman25
Forum Newbie
Posts: 8
Joined: Tue Jul 03, 2007 9:22 am
Location: PA

trouble with jpeg output

Post by bluesguitarman25 »

for some reason, when i switched everything from working with png files to jpegs, all of the sudden i get an output of the web address of the file i am running. can someone help? by the way, the script is designed to take several images and several pieces of text and merge them together, as in an id.

Code: Select all

mysql_select_db(user_info);
$id = $_POST['email'];
$sql2 = "SELECT * FROM user_info WHERE email ='$id'";
$result = mysql_query($sql2) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if (!$row) {
echo "<br />Nothing to print....";
}

$string = "DOB:     " . $row['dob'];
$string2 = $row['first_name'] . " " . $row['last_name'];
$string3 = $row['region'];
$string4 = $row['language'];
$string5 = $row['greeting'];
$string6 = $row['email'];
$id_num = "ID#  " . $row['id'];
$pix = $row['pix'];

header("Content-type:image/jpg");
$im     = imagecreatetruecolor('citizen-blank.jpg');
$logo = imagecreatefromgif('citizen-logo.gif');
$orange = imagecolorallocate($im, 220, 210, 60);
$black = imagecolorallocate($im, 0, 0, 0);
// next line = bg image, font size, x,y, string, color
// set ($im, 5, 20, 30...) for ID#
imagestring($im, 2, 157, 70, $string, $black);
imagestring($im, 2, 157, 80, $string2, $black);
imagestring($im, 2, 157, 90, $string3, $black);
imagestring($im, 2, 157, 100, $string4, $black);
imagestring($im, 2, 157, 110, $string5, $black);
imagestring($im, 2, 157, 120, $string6, $black);
imagestring($im, 5, 10, 30, $id_num, $orange);

// The pic file
// Set a maximum height and width
$width = 116;
$height = 165;

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

if ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
} else {
    $height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatetruecolor($pix);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagecopymerge($im, $image_p, 20, 67, 0, 0, $width, $height, 100);
imagecopymerge($im, $logo, 10, 137, 0, 0, 56, 56, 100);
// Output

imagejpeg($im);

?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: trouble with jpeg output

Post by superdezign »

bluesguitarman25 wrote:for some reason, when i switched everything from working with png files to jpegs, all of the sudden i get an output of the web address of the file i am running.
Output to where?


In order to debug this, get rid of the header and start echoing out each step.
BTW, I think the header is supposed to be image/jpeg, but I'm not sure if it'd make a difference.
bluesguitarman25
Forum Newbie
Posts: 8
Joined: Tue Jul 03, 2007 9:22 am
Location: PA

output to where?

Post by bluesguitarman25 »

the output is just supposed to be to the web page. as far as the header, i've tried both jpg and jpeg. ok, i'll go thru the pain in the a@# to echo each statement. i'll be back with another post when finished.
bluesguitarman25
Forum Newbie
Posts: 8
Joined: Tue Jul 03, 2007 9:22 am
Location: PA

imagecopyresampled() issue

Post by bluesguitarman25 »

ok, so on my localhost, this script works:

Code: Select all

mysql_select_db(test_db);
$id = $_POST['email'];
$sql2 = "SELECT * FROM user_info WHERE email ='$id'";
$result = mysql_query($sql2) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if (!$row) {
echo "<br />Nothing to print....";
}

$string = "DOB:     " . $row['dob'];
$string2 = $row['first_name'] . " " . $row['last_name'];
$string3 = $row['region'];
$string4 = $row['language'];
$string5 = $row['greeting'];
$string6 = $row['email'];
$id_num = "ID#  " . $row['id'];
$pix = $row['pix'];

header("Content-type:image/jpeg");
$im     = imagecreatefromjpeg('citizen-blank.jpg');
$logo = imagecreatefromgif('citizen-logo.gif');
$orange = imagecolorallocate($im, 220, 210, 60);
$black = imagecolorallocate($im, 0, 0, 0);
// next line = bg image, font size, x,y, string, color
// set ($im, 5, 20, 30...) for ID#
imagestring($im, 2, 157, 70, $string, $black);
imagestring($im, 2, 157, 80, $string2, $black);
imagestring($im, 2, 157, 90, $string3, $black);
imagestring($im, 2, 157, 100, $string4, $black);
imagestring($im, 2, 157, 110, $string5, $black);
imagestring($im, 2, 157, 120, $string6, $black);
imagestring($im, 5, 10, 30, $id_num, $orange);
// insert upload image here

// The pic file

// Set a maximum height and width
$width = 116;
$height = 165;

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

if ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
} else {
    $height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatefromjpeg($width, $height);
$image = imagecreatefromjpeg($pix);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagecopymerge($im, $image_p, 20, 67, 0, 0, $width, $height, 100);
imagecopymerge($im, $logo, 10, 137, 0, 0, 56, 56, 100);
// Output

imagejpeg($im);
BUT on my actual web server, i get the web address of the script printed to the screen rather than a pic generated by the script. i echoed all statements until i came up with a prob and the prob begins with

Code: Select all

imagecopyresampled()
this is where i start getting the web address output. i checked the gd versions between my dev env and the actual server and all is the same except the t1lib.

localhost

Code: Select all

var_dump(gd_info());
:
array(12) { ["GD Version"]=> string(27) "bundled (2.0.28 compatible)" ["FreeType Support"]=> bool(true) ["FreeType Linkage"]=> string(13) "with freetype" ["T1Lib Support"]=> bool(true) ["GIF Read Support"]=> bool(true) ["GIF Create Support"]=> bool(true) ["JPG Support"]=> bool(true) ["PNG Support"]=> bool(true) ["WBMP Support"]=> bool(true) ["XPM Support"]=> bool(false) ["XBM Support"]=> bool(true) ["JIS-mapped Japanese Font Support"]=> bool(false) }

live server:
array(11) { ["GD Version"]=> string(27) "bundled (2.0.28 compatible)" ["FreeType Support"]=> bool(true) ["FreeType Linkage"]=> string(13) "with freetype" ["T1Lib Support"]=> bool(false) ["GIF Read Support"]=> bool(true) ["GIF Create Support"]=> bool(true) ["JPG Support"]=> bool(true) ["PNG Support"]=> bool(true) ["WBMP Support"]=> bool(true) ["XBM Support"]=> bool(true) ["JIS-mapped Japanese Font Support"]=> bool(false) }

no idea why this is not working.
bluesguitarman25
Forum Newbie
Posts: 8
Joined: Tue Jul 03, 2007 9:22 am
Location: PA

Fixed!!!

Post by bluesguitarman25 »

ok, all i had to do was change

Code: Select all

imagecreatefromjpeg();
to

Code: Select all

imagecreatetruecolor();
on line 52.

so here it is:

Code: Select all

mysql_select_db(user_info);
$id = $_POST['email'];
$sql2 = "SELECT * FROM user_info WHERE email ='$id'";
$result = mysql_query($sql2) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if (!$row) {
echo "<br />Nothing to print....";
}

$string = "DOB:     " . $row['dob'];
$string2 = $row['firstname'] . " " . $row['lastname'];
$string3 = $row['region'];
$string4 = $row['language'];
$string5 = $row['greeting'];
$string6 = $row['email'];
$id_num = "ID#  " . $row['id'];
$pix = $row['pix'];

header("Content-type:image/jpeg");
$im     = imagecreatefromjpeg('citizen-blank.jpg');
$logo = imagecreatefromgif('citizen-logo.gif');
$orange = imagecolorallocate($im, 220, 210, 60);
$black = imagecolorallocate($im, 0, 0, 0);
// next line = bg image, font size, x,y, string, color
// set ($im, 5, 20, 30...) for ID#
imagestring($im, 2, 157, 70, $string, $black);
imagestring($im, 2, 157, 80, $string2, $black);
imagestring($im, 2, 157, 90, $string3, $black);
imagestring($im, 2, 157, 100, $string4, $black);
imagestring($im, 2, 157, 110, $string5, $black);
imagestring($im, 2, 157, 120, $string6, $black);
imagestring($im, 5, 10, 30, $id_num, $orange);

// insert upload image here

// The pic file

// Set a maximum height and width
$width = 116;
$height = 165;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($pix) or die("could not get image dimensions");
if ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
} else {
    $height = ($width / $width_orig) * $height_orig;
}

// Resample
                 // $image_p's function had to be changed from imagecreatefromjpeg()
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($pix);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig) or die("could not resample");
imagecopymerge($im, $image_p, 20, 67, 0, 0, $width, $height, 100);
imagecopymerge($im, $logo, 10, 137, 0, 0, 56, 56, 100);

// Output
imagejpeg($im);
Post Reply