473,466 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Creating black images when trying to create thumbnails

Hi all,

I am a newbie at this. I am trying to create thumbnails. However, I
am only creating black image with the correct thumbnail size. I am
using php 5.1.6 and GD 2.0.28. Below is the code that I am using.
Can any one help?

Thanks,

Alanna

=================================

function createThumbnail($imageDirectory, $imageName, $thumbDirectory,
$thumbWidth)
{
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);

$thumbHeight =200;
$thumbWidth =100;

if($origWidth $origHeight && $thumbHeight < $origHeight){
$thumbHeight = $origHeight / ($origWidth / $thumbWidth);
} else if ($origWidth < $origHeight && $thumbWidth < $origWidth)
{
$thumbWidth = $origWidth / ($origHeight / $thumbHeight);
} else {
$thumbWidth = $origWidth;
$thumbHeight = $origHeight;
}
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);

imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth,
$thumbHeight, origWidth, $origHeight);

imagejpeg($thumbImg, "$thumbDirectory/$imageName");
}

if(!file_exists("/home/alanna/public_html/pics/thumb/")){
if(!mkdir("/home/alanna/public_html/pics/thumb/")) {
echo "error making thumbs directory";
}
}

if(file_exists("/home/alanna/public_html/pics/thumb/". $imagename)){
echo "File already exists";
}
else
{

createThumbnail("/home/alanna/public_html/pics/full/", $imagename, "/
home/alanna/public_html/pics/thumb/", 100);

}

Feb 5 '07 #1
1 2840
am********@yahoo.com wrote:
Hi all,

I am a newbie at this. I am trying to create thumbnails. However, I
am only creating black image with the correct thumbnail size. I am
using php 5.1.6 and GD 2.0.28. Below is the code that I am using.
Can any one help?

Thanks,

Alanna

=================================

function createThumbnail($imageDirectory, $imageName, $thumbDirectory,
$thumbWidth)
{
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);

$thumbHeight =200;
$thumbWidth =100;

if($origWidth $origHeight && $thumbHeight < $origHeight){
$thumbHeight = $origHeight / ($origWidth / $thumbWidth);
} else if ($origWidth < $origHeight && $thumbWidth < $origWidth)
{
$thumbWidth = $origWidth / ($origHeight / $thumbHeight);
} else {
$thumbWidth = $origWidth;
$thumbHeight = $origHeight;
}
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);

imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth,
$thumbHeight, origWidth, $origHeight);

imagejpeg($thumbImg, "$thumbDirectory/$imageName");
}

if(!file_exists("/home/alanna/public_html/pics/thumb/")){
if(!mkdir("/home/alanna/public_html/pics/thumb/")) {
echo "error making thumbs directory";
}
}

if(file_exists("/home/alanna/public_html/pics/thumb/". $imagename)){
echo "File already exists";
}
else
{

createThumbnail("/home/alanna/public_html/pics/full/", $imagename, "/
home/alanna/public_html/pics/thumb/", 100);

}
Hi,

To debug imagecreationscripts try the following:
1) Set the header for mimetype from image/jpeg (or whatever it is) to
text/html
2) Run your script.

That way your browser can actually display any errors in your script.
If you leave the mimetype to an image, the browser will try to interpret
your echos as an image, maybe resulting in a black image.

Of course, when you fixed your errors, you can switch back to image/jpeg to
check if PHP actually delivers an image you like.
Regards,
Erwin Moller
Regards,
Erwin Moller
Feb 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lawrence | last post by:
So far I've got the code you see below. I've not yet figured out how to change the name of the file. I'm creating several images of different sizes. I need to give them all different names. How do...
5
by: Amir | last post by:
When prefetching images, why do I need the "if (document.images)" statement? if (document.images) { pic1on= new Image(300,300); pic1on.src="pic1.gif"; pic2on= new Image();...
1
by: Ghanashyam | last post by:
Dotnet Framework 1.1 is installed on my computer running on Windows 2000 Professional.Visual Studio >net 2003 is installed.When I try to create an ASP.NET project I get following error. The web...
1
by: intl04 | last post by:
I am getting strange print-related error messages when trying to create (not print!) reports. For example, when I click 'new' to create a report then choose 'design view', I get an error message...
4
by: Thomas Richter | last post by:
Hi, I can't get of the black flicker when I resize my form. this = Mainform : System.Windows.Forms.Form If I set the size from 300 to 500 I see for ca 500ms some black areas. I try to solve it...
1
by: יוני גולדברג | last post by:
Hi, While trying to create new directory i recieve the following error message: "System.IO.DirectoryNotFoundException: Could not find a part of the path "\\premfs16\sites". The path exists, even...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
2
by: Krustov | last post by:
The question of how to create thumbnails with clickable links gets asked on this newsgroup every so often - like clock work . Just really posing this script here for google to archive it & so...
2
by: GK | last post by:
Hi all, I wish to know , how i can create thumbnails automatically using javascripts. It should reduce the size as well as pixel resolution (ie, a 1 mb image of 1280 x 800 px can be reduced to...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.