 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
08-03-2008, 12:56 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Jun 2005
Posts: 21
Thanks: 1
|
Looking for transparency help
I have a script I kind of tweaked which would resize my images, jpg, gif, or pngs. The script uploads a copy of the source and then a resized thumbnail.
Only issue is that the thumbnail of a png has a black background and I cant figure out how to keep the original transparency.
PHP Code:
<?php
########################################
# include the getNextFilename function #
########################################
include('getNextFilename5.php'); // use getNextFilename4.php on a PHP 4 server
########################################
// define constants
define('THUMBS_DIR', '/home/golden/public_html/images/clubs/thumbs/');
define('MAX_WIDTH', 120);
define('MAX_HEIGHT', 90);
// process the uploaded image
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
$original = $_FILES['image']['tmp_name'];
// begin by getting the details of the original
list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio
if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
$ratio = 1;
}
elseif ($width > $height) {
$ratio = MAX_WIDTH/$width;
}
else {
$ratio = MAX_HEIGHT/$height;
}
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));
########################################################
# getNextFilename() requires the file type as a string #
# so begin by converting it to a string assigned to $t #
########################################################
switch($type) {
case 1:
$t = 'gif';
break;
case 2:
$t = 'jpg';
break;
case 3:
$t = 'png';
}
################################
# get new name for upload file #
################################
$newName = getNextFilename5(UPLOAD_DIR, $name, $t); // use getNextFilename4() on a PHP 4 server
################################################
# use the new name instead of the existing one #
################################################
// move the temporary file to the upload folder
$moved = move_uploaded_file($original, UPLOAD_DIR.$newName);
if ($moved) {
$result = "$newName successfully uploaded; ";
$original = UPLOAD_DIR.$newName;
}
else {
$result = 'Problem uploading '.$_FILES['image']['name'].'; ';
}
################################################
// create an image resource for the original
switch($type) {
case 1:
$source = @ imagecreatefromgif($original);
if (!$source) {
$result = 'Cannot process GIF files. Please use JPEG or PNG.';
}
break;
case 2:
$source = imagecreatefromjpeg($original);
break;
case 3:
$source = imagecreatefrompng($original);
break;
default:
$source = NULL;
$result = 'Cannot identify file type.';
}
// make sure the image resource is OK
if (!$source) {
$result = 'Problem copying original';
}
else {
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
switch($type) {
case 1:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.gif');
######################################################
if (function_exists('imagegif')) {
$success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
$thumb_name = $name.'_thb.gif';
}
else {
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50);
$thumb_name = $name.'_thb.jpg';
}
break;
case 2:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.jpg');
######################################################
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
$thumb_name = $name.'_thb.jpg';
break;
case 3:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.png');
######################################################
$success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
$thumb_name = $name.'_thb.png';
}
if ($success) {
$result .= "$thumb_name created";
}
else {
$result .= 'Problem creating thumbnail';
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}
}
?>
|
|
|
|
08-04-2008, 03:54 PM
|
#2 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
|
In order to preserve the translucency/transparency in a PNG file, you will need to make sure that you enable the alpha channel as follows...
PHP Code:
<?php
########################################
# include the getNextFilename function #
########################################
include('getNextFilename5.php'); // use getNextFilename4.php on a PHP 4 server
########################################
// define constants
define('THUMBS_DIR', '/home/golden/public_html/images/clubs/thumbs/');
define('MAX_WIDTH', 120);
define('MAX_HEIGHT', 90);
// process the uploaded image
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
$original = $_FILES['image']['tmp_name'];
// begin by getting the details of the original
list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio
if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
$ratio = 1;
}
elseif ($width > $height) {
$ratio = MAX_WIDTH/$width;
}
else {
$ratio = MAX_HEIGHT/$height;
}
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));
########################################################
# getNextFilename() requires the file type as a string #
# so begin by converting it to a string assigned to $t #
########################################################
switch($type) {
case 1:
$t = 'gif';
break;
case 2:
$t = 'jpg';
break;
case 3:
$t = 'png';
}
################################
# get new name for upload file #
################################
$newName = getNextFilename5(UPLOAD_DIR, $name, $t); // use getNextFilename4() on a PHP 4 server
################################################
# use the new name instead of the existing one #
################################################
// move the temporary file to the upload folder
$moved = move_uploaded_file($original, UPLOAD_DIR.$newName);
if ($moved) {
$result = "$newName successfully uploaded; ";
$original = UPLOAD_DIR.$newName;
}
else {
$result = 'Problem uploading '.$_FILES['image']['name'].'; ';
}
################################################
// create an image resource for the original
switch($type) {
case 1:
$source = @ imagecreatefromgif($original);
if (!$source) {
$result = 'Cannot process GIF files. Please use JPEG or PNG.';
}
break;
case 2:
$source = imagecreatefromjpeg($original);
break;
case 3:
$source = imagecreatefrompng($original);
// enable alpha channnel in PNG file
imageAlphaBlending($source, true);
imageSaveAlpha($source, true);
break;
default:
$source = NULL;
$result = 'Cannot identify file type.';
}
// make sure the image resource is OK
if (!$source) {
$result = 'Problem copying original';
}
else {
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
switch($type) {
case 1:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.gif');
######################################################
if (function_exists('imagegif')) {
$success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
$thumb_name = $name.'_thb.gif';
}
else {
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50);
$thumb_name = $name.'_thb.jpg';
}
break;
case 2:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.jpg');
######################################################
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
$thumb_name = $name.'_thb.jpg';
break;
case 3:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.png');
######################################################
$success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
$thumb_name = $name.'_thb.png';
}
if ($success) {
$result .= "$thumb_name created";
}
else {
$result .= 'Problem creating thumbnail';
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}
}
?>
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
|
|
|
|
08-04-2008, 06:03 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Jun 2005
Posts: 21
Thanks: 1
|
Bill, I've added the new lines that you had and I still have a black background around my resized thumbnail.
|
|
|
|
08-04-2008, 06:35 PM
|
#4 (permalink)
|
|
The Wanderer
Join Date: Jun 2005
Posts: 21
Thanks: 1
|
I ended up getting it to work using..
PHP Code:
/* making the new image transparent */ $background = imagecolorallocate($thumb, 0, 0, 0); ImageColorTransparent($thumb, $background); // make the new temp image all transparent imagealphablending($thumb, false); // turn off the alpha blending to keep the alpha channel
// create the resized copy imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
But the colors seem a tad lighter in the new thumbnail? Is that just due to the resizing and keeping transparency?
|
|
|
|
08-05-2008, 06:12 PM
|
#5 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
|
It may seem a little muted, because I think that the ImageColorTransparent() call will change all the blacks to transparent for the image.
I looked at this some more. Try changing the following.
php Code:
// Change this: $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// To this: $thumb = imagecreate($thumb_width, $thumb_height);
The reason is that imagecreatetruecolor returns an image resource with the background color as black. imagecreate will just return a blank image resource.
I tried this out on my machine with a png with some transparency it in. It seemed to work correctly.
PHP Code:
<?php
########################################
# include the getNextFilename function #
########################################
include('getNextFilename5.php'); // use getNextFilename4.php on a PHP 4 server
########################################
// define constants
define('THUMBS_DIR', '/home/golden/public_html/images/clubs/thumbs/');
define('MAX_WIDTH', 120);
define('MAX_HEIGHT', 90);
// process the uploaded image
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
$original = $_FILES['image']['tmp_name'];
// begin by getting the details of the original
list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio
if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
$ratio = 1;
}
elseif ($width > $height) {
$ratio = MAX_WIDTH/$width;
}
else {
$ratio = MAX_HEIGHT/$height;
}
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));
########################################################
# getNextFilename() requires the file type as a string #
# so begin by converting it to a string assigned to $t #
########################################################
switch($type) {
case 1:
$t = 'gif';
break;
case 2:
$t = 'jpg';
break;
case 3:
$t = 'png';
}
################################
# get new name for upload file #
################################
$newName = getNextFilename5(UPLOAD_DIR, $name, $t); // use getNextFilename4() on a PHP 4 server
################################################
# use the new name instead of the existing one #
################################################
// move the temporary file to the upload folder
$moved = move_uploaded_file($original, UPLOAD_DIR.$newName);
if ($moved) {
$result = "$newName successfully uploaded; ";
$original = UPLOAD_DIR.$newName;
}
else {
$result = 'Problem uploading '.$_FILES['image']['name'].'; ';
}
################################################
// create an image resource for the original
switch($type) {
case 1:
$source = @ imagecreatefromgif($original);
if (!$source) {
$result = 'Cannot process GIF files. Please use JPEG or PNG.';
}
break;
case 2:
$source = imagecreatefromjpeg($original);
break;
case 3:
$source = imagecreatefrompng($original);
break;
default:
$source = NULL;
$result = 'Cannot identify file type.';
}
// make sure the image resource is OK
if (!$source) {
$result = 'Problem copying original';
}
else {
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
// create an image resource for the thumbnail
$thumb = imagecreate($thumb_width, $thumb_height);
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
switch($type) {
case 1:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.gif');
######################################################
if (function_exists('imagegif')) {
$success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
$thumb_name = $name.'_thb.gif';
}
else {
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50);
$thumb_name = $name.'_thb.jpg';
}
break;
case 2:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.jpg');
######################################################
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
$thumb_name = $name.'_thb.jpg';
break;
case 3:
######################################################
# use the new name as the basis for the thumb's name #
######################################################
$name = basename($newName, '.png');
######################################################
$success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
$thumb_name = $name.'_thb.png';
}
if ($success) {
$result .= "$thumb_name created";
}
else {
$result .= 'Problem creating thumbnail';
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}
}
?>
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|