GD image resize gives black result??!!

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

User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

YEAAAHHHH!!!!!!!!!!!
markl999 Rulez man!

For persons who have the same problem later, i now have this code, and it works:

<?php
# Constants
define('IMAGE_BASE', '/home/httpd/vhosts/bevibed.be/httpdocs/artists/Piddle/');
//define('IMAGE_BASE', //);
define('MAX_WIDTH', 50);
define('MAX_HEIGHT', 50);

# Get image location
$image_file = 'hoesthumb.jpg';
$image_path = IMAGE_BASE . "/$image_file";

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);

# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);

# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}

# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>


Thanx to all the people who helped me :!:
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

Ok, that works, but now, if i replace the directory with a variabele that contains a query-result, it doesn't work anymore.
Could it be that it will never work like that, or is it just me?

Here's the code:

<?php
mysql_select_db($database_memberships, $memberships);
$query_Recordset64 = "SELECT * FROM bands WHERE band_name = 'MILK THE FISH!'";
$Recordset64 = mysql_query($query_Recordset64, $memberships) or die(mysql_error());
$row_Recordset64 = mysql_fetch_assoc($Recordset64);
$totalRows_Recordset64 = mysql_num_rows($Recordset64);

$bandnaam = $row_Recordset64['band_name'];
$imagenaam = $row_Recordset64['band_imagename'];
$genre1 = $row_Recordset64['band_hoofdgenre'];
$genre2 = $row_Recordset64['band_subgenre'];
echo $bandnaam;
echo $imagenaam;

# Constants
define('IMAGE_BASE', '/home/httpd/vhosts/bevibed.be/httpdocs/artists/$bandnaam/');
//define('IMAGE_BASE', //);
define('MAX_WIDTH', 100);
define('MAX_HEIGHT', 100);

# Get image location
$image_file = '$imagenaam';
$image_path = IMAGE_BASE . "/$image_file";
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

define('IMAGE_BASE', '/home/httpd/vhosts/bevibed.be/httpdocs/artists/$bandnaam/');

Variables don't get interpolated inside single quotes. For example:

Code: Select all

$foo = 'hello';
echo '$foo'; // this will iterally output $foo
echo "$foo"; //this will output hello
So you need:
define('IMAGE_BASE', "/home/httpd/vhosts/bevibed.be/httpdocs/artists/$bandnaam/");

and:
$image_file = $imagenaam;
^^doesn't need any quotes as it's just a direct assignment. In fact you could leave that line out and just do:
$image_path = IMAGE_BASE . "/$imagenaam";
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

If i want these image to show now, its ok, but if i include that page into another page with layout, i get following error:


ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC $.' ",#(7),01444'9=82<.342ÿÛC 2!!22222222222222222222222222222222222222222222222222ÿÀÈÈ"ÿÄ ÿĵ}!1AQa"q2
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

Or is there a way where you can set the header content to only that included file? Not the the major-page.
So that the the major file has default content, and the included image/jpeg.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Not sure you should be including it.
<img src="foo.php"> works fine, presuming your image resizing script is called foo.php
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

That works, but the problem is that it now gets the imagename from a db, and if i want to echo the name on the screen, it doesnt work.
I only can show the picture self, nothing else.

Here's the code i'm working on now:


<?php require_once('../Connections/memberships.php'); ?>
<?php
mysql_select_db($database_memberships, $memberships);
$query_Recordset60 = "SELECT * FROM bands WHERE band_active = '1' and band_image != '' ORDER BY rand()";
$Recordset60 = mysql_query($query_Recordset60, $memberships) or die(mysql_error());
$row_Recordset60 = mysql_fetch_assoc($Recordset60);
$totalRows_Recordset60 = mysql_num_rows($Recordset60);

$id = $row_Recordset60['band_id'];


mysql_select_db($database_memberships, $memberships);
$query_Recordset64 = "SELECT * FROM bands WHERE band_id = $id";
$Recordset64 = mysql_query($query_Recordset64, $memberships) or die(mysql_error());
$row_Recordset64 = mysql_fetch_assoc($Recordset64);
$totalRows_Recordset64 = mysql_num_rows($Recordset64);

$bandnaam = $row_Recordset64['band_name'];
$imagenaam = $row_Recordset64['band_imagename'];
$genre1 = $row_Recordset64['band_hoofdgenre'];
$genre2 = $row_Recordset64['band_subgenre'];


# Constants
define('IMAGE_BASE', "/home/httpd/vhosts/bevibed.be/httpdocs/artists/$bandnaam/");
//define('IMAGE_BASE', //);
define('MAX_WIDTH', 200);
define('MAX_HEIGHT', 200);

# Get image location
$image_file = $imagenaam;
$image_path = IMAGE_BASE . "/$image_file";

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);

# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);

# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}

# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

Sorry, its fine. I did it like you say, with an img tag, and now it works.
Thanks again. :)
Post Reply