473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Thumbnails - Halving width but doubling height?

73
Hello, I've been searching for a tutorial to create thumbnails for a while and finally found one that works for me. It's doing everything it's suppose to except one thing.

I created a test image 600px wide, 700px high, so to get 300px width it would need to half that, as well as the 700px height. It's resizing the width to half the size (300px) except instead of halfing the height to 350px, it doubles it to 1400px, which obviously causes the image to come out very wrong.

Here's the code I'm using:

Expand|Select|Wrap|Line Numbers
  1. function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
  2. {
  3. $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
  4. $origWidth = imagesx($srcImg);
  5. $origHeight = imagesy($srcImg);
  6.  
  7. $ratio = $origWidth / $thumbWidth;
  8. $thumbHeight = $origHeight * $ratio;
  9.  
  10. $thumbImg = imagecreate($thumbWidth, $thumbHeight);
  11. imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($thumbImg), imagesy($thumbImg));
  12.  
  13. imagejpeg($thumbImg, "$thumbDirectory/$imageName");
  14. }
  15.  
  16. createThumbnail("uploads/examples", "$newname", "uploads/examples/thumbnails", 300);
  17.  
Thanks,
Jeigh.
Aug 11 '07 #1
16 1518
jx2
228 100+
change line 7
[php]
ratio = $thumbWidth / $origWidth ;
[/php]

regards
jx2
Aug 11 '07 #2
Atli
5,058 Expert 4TB
Hi.

At line 8, you should divide, not multiply.
Aug 11 '07 #3
jx2
228 100+
Hi.

At line 8, you should divide, not multiply.
LOL i think you're confusing him
yeah
it can be your way

regards
jx2
Aug 11 '07 #4
Atli
5,058 Expert 4TB
LOL i think you're confusing him
yeah
it can be your way

regards
jx2
Well, there are two ways I would consider using for this.

The first one. I've used a number of times.
Expand|Select|Wrap|Line Numbers
  1. $ratio = $originalHeight / $originalWidth;
  2. $thumbHeight = $thumbWidth * $ratio;
  3.  
The second one. A little limited but works in this case.
Expand|Select|Wrap|Line Numbers
  1. $ratio = $originalWidth / $thumbWidth;
  2. $thumbHeight = $originalHeight / $ratio;
  3.  
I'ts like Jeigh was using the second one but mixed up the second line. Has probbly happend to all of us at some point.
Aug 11 '07 #5
Jeigh
73
Thanks for that guys, it worked but it seems to crop the image instead of resizing it, am I using the wrong sort of script or can this be modified to do that?
Aug 11 '07 #6
jx2
228 100+
Thanks for that guys, it worked but it seems to crop the image instead of resizing it, am I using the wrong sort of script or can this be modified to do that?
look here

you need two more parameters in line 11 for sorce height and width

regards
jx2
Aug 11 '07 #7
Atli
5,058 Expert 4TB
look here

you need two more parameters in line 11 for sorce height and width

regards
jx2
This one is pretty much the same imagecopyresampled(), except this one not only resizes the image, it also resamples it, so it gives you better quality.
Aug 11 '07 #8
jx2
228 100+
This one is pretty much the same imagecopyresampled(), except this one not only resizes the image, it also resamples it, so it gives you better quality.
thx Atli
nice job i didnt know that one ;-)

regards
jx2
Aug 11 '07 #9
Jeigh
73
I had a look at the links you gave me and now I'm using:

Expand|Select|Wrap|Line Numbers
  1. function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
  2. {
  3. $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
  4. $origWidth = imagesx($srcImg);
  5. $origHeight = imagesy($srcImg);
  6.  
  7. list($width, $height) = getimagesize($newname);
  8.  
  9. $ratio = $origWidth / $thumbWidth;
  10. $thumbHeight = $origHeight / $ratio;
  11.  
  12. $thumbImg_p = imagecreatetruecolor($thumbWidth, $thumbHeight);
  13.  
  14. imagecopyresampled($thumbImg_p, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($thumbImg), imagesy($thumbImg));
  15.  
  16. imagejpeg($thumbImg_p, "$thumbDirectory/$imageName");
  17. }
  18.  
  19. createThumbnail("uploads/examples", "$newname", "uploads/examples/thumbnails", 300);  
  20.  
But I'm still getting the same result, the correct size but it's cropped instead of resized. Obviously I'm missing somthing I just dont know what :P

(This is the first time I've used anything with GD)
Aug 12 '07 #10
Atli
5,058 Expert 4TB
The last two parameters of the imagecopyresampled() function, you use in line 14, are supposed to be the size of the original image, not the thumbnail.

Try this:
Expand|Select|Wrap|Line Numbers
  1. imagecopyresampled($thumbImg_p, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
  2.  
Aug 12 '07 #11
Jeigh
73
Ah, thanks a lot Atli. Works great now :D

Just trying to get it to work with .png and .gif as well as just jpeg using this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
  3. {
  4.  
  5. switch ($ext) {
  6. case ".jpeg":
  7. $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
  8.     break;
  9. case ".png":
  10. $srcImg = imagecreatefrompng("$imageDirectory/$imageName");
  11.     break;
  12. case ".gif":
  13. $srcImg = imagecreatefromgif("$imageDirectory/$imageName");
  14.     break;
  15. }
  16.  
  17. $origWidth = imagesx($srcImg);
  18. $origHeight = imagesy($srcImg);
  19.  
  20. list($width, $height) = getimagesize($newname);
  21.  
  22. $ratio = $origWidth / $thumbWidth;
  23. $thumbHeight = $origHeight / $ratio;
  24.  
  25. $thumbImg_p = imagecreatetruecolor($thumbWidth, $thumbHeight);
  26. $thumbImg = imagecreate($thumbWidth, $thumbHeight);
  27. /*imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($thumbImg), imagesy($thumbImg));*/
  28.  
  29. imagecopyresampled($thumbImg_p, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
  30.  
  31. switch ($ext) {
  32. case ".jpeg":
  33. imagejpeg($thumbImg_p, "$thumbDirectory/$imageName");
  34.     break;
  35. case ".png":
  36. imagepng($thumbImg_p, "$thumbDirectory/$imageName");
  37.     break;
  38. case ".gif":
  39. imagegif($thumbImg_p, "$thumbDirectory/$imageName");
  40.     break;
  41. }
  42.  
  43.  
  44. }
  45.  
  46. createThumbnail("uploads/examples", "$newname", "uploads/examples/thumbnails", 300);  
  47.  
Which brings up an error for just about every function I was using, any clues?
Aug 13 '07 #12
Atli
5,058 Expert 4TB
You are using a variable called $ext in the two switch statements that has no value. I would guess that's the problem.
Aug 13 '07 #13
Jeigh
73
Sorry, forgot to mention, $ext is defined earlier in the script, which is just the extension of the file:

Expand|Select|Wrap|Line Numbers
  1.  $ext = strrchr($_FILES['uploaded_image']['name'], "."); 
  2.         if ($ext != ".gif" AND $ext != ".jpg" AND $ext != ".jpeg" AND $ext != ".bmp" AND $ext != ".GIF" AND $ext != ".JPG" AND $ext != ".JPEG" AND $ext != ".BMP" AND $ext != ".png") { 
  3.             $error = "your file was an unacceptable type.<br />";
  4.  
Aug 14 '07 #14
jx2
228 100+
Sorry, forgot to mention, $ext is defined earlier in the script, which is just the extension of the file:

Expand|Select|Wrap|Line Numbers
  1.  $ext = strrchr($_FILES['uploaded_image']['name'], "."); 
  2.         if ($ext != ".gif" AND $ext != ".jpg" AND $ext != ".jpeg" AND $ext != ".bmp" AND $ext != ".GIF" AND $ext != ".JPG" AND $ext != ".JPEG" AND $ext != ".BMP" AND $ext != ".png") { 
  3.             $error = "your file was an unacceptable type.<br />";
  4.  
is it a code writen whitin the function or $ext is global variable?

if this is code writen outside this function you need to make sure $ext exist in a function

[php]global $ext;[/php]

line 22[php]$thumbImg = imagecreate($thumbWidth, $thumbHeight);[/php]
you never uuse this variable what this is for?

i strongly recomend you try to write this function on your own (its not very difficukt!!) so you can know how it works

regards
jx2
Aug 14 '07 #15
Atli
5,058 Expert 4TB
Sorry, forgot to mention, $ext is defined earlier in the script, which is just the extension of the file:

Expand|Select|Wrap|Line Numbers
  1.  $ext = strrchr($_FILES['uploaded_image']['name'], "."); 
  2.         if ($ext != ".gif" AND $ext != ".jpg" AND $ext != ".jpeg" AND $ext != ".bmp" AND $ext != ".GIF" AND $ext != ".JPG" AND $ext != ".JPEG" AND $ext != ".BMP" AND $ext != ".png") { 
  3.             $error = "your file was an unacceptable type.<br />";
  4.  
If this variable is created outside the function it is likely that it will not work inside your function. You must either make it global, as Jx2 suggessted, or somehow import it into the function.
Personally I would pass it into the function as a parameter.
Aug 14 '07 #16
Jeigh
73
Thanks guys, using global fixed it :)
Aug 15 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ohaya | last post by:
Hi, I was wondering what happens when width and height in a style (e.g., in a DIV) are set to '0px'? For example something like: style="position: absolute; width: 0px; height: 0px;" ...
3
by: coolsti | last post by:
I need some help here. I am making an application which allows a user to look at a series of picture files one at a time, and enter the outcome of various visual tests to a database. The...
2
by: Clemens Quoss | last post by:
Hello, I just encountered a strange thing when switching the doctype on and off in the following html testpage (with IE 6.0): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
2
by: Robson Carvalho Machado | last post by:
Dear friends, I'm dynamically creating a Hyperlink with spacer.gif as ImageURL that is an 1px transparent image only to determine link position, but as I create this link dynamically I could not...
3
by: Sven C. Koehler | last post by:
Hello, the html below displays a box (#container) with a #footer and an #image inside. Is there any way how I could get rid of declaring max-width and max-height for the #image? The #image...
4
by: SM | last post by:
Hello, I've created a Unordered list with images using the Javascript DOM, and i want to set up the width and height attributes of the image... The problem is that most images vary in size....
2
by: Atul | last post by:
I am unable to find image height and width in mozilla firefox. My code is working in IE but not in Mozilla. How can i find image width and height in mozilla? function check(sel) { if(sel != "")...
1
by: tader | last post by:
hello everyone! so i got this question can anyone help me with this so i got this flash file test.swf it's width is 350 and height 200 and teen you need to put that flash into the web you need...
2
by: liketofindoutwhy | last post by:
For Firefox, we can get the computed style of an Img element, using the script in http://www.quirksmode.org/dom/getstyles.html so even if we set only the width of an Image in CSS, we will get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.