943,723 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1271
  • PHP RSS
Apr 21st, 2009
0

How to resize images

Expand Post »
Can any one help me how to resize images in php with out changing its original quality,that is if an image with size 100/100 when it reduced to 40/30 or any our required dimensions with out changing its quality .
Similar Threads
Reputation Points: 9
Solved Threads: 2
Light Poster
nathenastle is offline Offline
28 posts
since Mar 2009
Apr 21st, 2009
0

Re: How to resize images

Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009
Apr 21st, 2009
0

Re: How to resize images

Can any one help me how to resize images in php with out changing its original quality,that is if an image with size 100/100 when it reduced to 40/30 or any our required dimensions with out changing its quality .

Please check blow Pices of Code
PHP Syntax (Toggle Plain Text)
  1. $tmpFName = $_FILES["UploadFloorPlane"]["tmp_name"];
  2. $fType = $_FILES["UploadFloorPlane"]["type"];
  3. $fSize = $_FILES["UploadFloorPlane"]["size"];
  4. list($width, $height, $type, $attr) = getimagesize($tmpFName);
  5. if($width > 0) {
  6. $fType = image_type_to_mime_type($type);
  7. $img_valid = "true";
  8. } else
  9. $img_valid = "false";
  10.  
  11. if($img_valid == "true"){
  12. if(($fType == 'image/jpeg') || ($fType == 'image/pjpeg')){
  13. $pExt = ".jpg";
  14. }
  15. elseif($fType == 'image/gif'){
  16. $pExt=".gif";
  17. }
  18. elseif($fType == 'image/png'){
  19. $pExt=".png";
  20. }
  21. else{
  22. $result = -2;
  23. }
  24. $rand=rand(0,10000);
  25. $Enlarge = "ImageName";
  26. $thumb = "ImageName_thumb";
  27. $origImage = $Enlarge ;
  28. copy($tmpFName,$origImage.$pExt);
  29. chmod ($origImage.$pExt, 0755); // octal; correct value of mode
  30. $NormalThumbImgName = $Enlarge.$pExt;
  31. $SmallThumbImgName = $thumb.$pExt;
  32. $DestPath = getcwd();
  33. $objThumbnail = new ThumbNail();
  34. $objThumbnail->CreateThumb($SmallThumbImgName,$DestPath.$origImage.$pExt,$DestPath,40,30,false,false);


Download the below Url Class files
http://www.phpclasses.org/browse/file/4883.html


BaburajSns
Securenext Softwares Pvt
Last edited by Ezzaral; Apr 21st, 2009 at 1:13 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
BaburajSNS is offline Offline
5 posts
since Apr 2009
Apr 21st, 2009
0

Re: How to resize images

One more point...
By using our php codes..
Changing from 100X100 image to 40X50 image will result into same quality image...But vice versa will give corrupted image..

So make it note when your are going develop in your projects..
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Apr 21st, 2009
0
Re: How to resize images
Thanks for your replay but where is the code iasked can you please tell me
Reputation Points: 9
Solved Threads: 2
Light Poster
nathenastle is offline Offline
28 posts
since Mar 2009
Apr 21st, 2009
0

Re: How to resize images

PHP Syntax (Toggle Plain Text)
  1. $tmpFName = $_FILES["UploadFloorPlane"]["tmp_name"];
  2. $fType = $_FILES["UploadFloorPlane"]["type"];
  3. $fSize = $_FILES["UploadFloorPlane"]["size"];
  4. list($width, $height, $type, $attr) = getimagesize($tmpFName);
  5. if($width > 0) {
  6. $fType = image_type_to_mime_type($type);
  7. $img_valid = "true";
  8. } else
  9. $img_valid = "false";
  10.  
  11. if($img_valid == "true"){
  12. if(($fType == 'image/jpeg') || ($fType == 'image/pjpeg')){
  13. $pExt = ".jpg";
  14. }
  15. elseif($fType == 'image/gif'){
  16. $pExt=".gif";
  17. }
  18. elseif($fType == 'image/png'){
  19. $pExt=".png";
  20. }
  21. else{
  22. $result = -2;
  23. }
  24. $rand=rand(0,10000);
  25. $Enlarge = "ImageName";
  26. $thumb = "ImageName_thumb";
  27. $origImage = $Enlarge ;
  28. copy($tmpFName,$origImage.$pExt);
  29. chmod ($origImage.$pExt, 0755); // octal; correct value of mode
  30. $NormalThumbImgName = $Enlarge.$pExt;
  31. $SmallThumbImgName = $thumb.$pExt;
  32. $DestPath = getcwd();
  33. $objThumbnail = new ThumbNail();
  34. $objThumbnail->CreateThumb($SmallThumbImgName,$DestPath.$origImage.$pExt,$DestPath,40,30,false,false);


Download the CreateThumb Class files function
http://www.daniweb.com/forums/post85...tml#post850887
Last edited by Ezzaral; Apr 21st, 2009 at 1:15 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
BaburajSNS is offline Offline
5 posts
since Apr 2009
Apr 22nd, 2009
0

Re: How to resize images

Hi, I have made a function that will resize images and is as follows:
php Syntax (Toggle Plain Text)
  1. <?php
  2. function resize_image($filename,$newwidth,$newheight) {
  3. if (!file_exists($filename) || !in_array(preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename)),array('png','jpg','jpeg','gif','xbm','xpm','wbmp'))) {
  4. //note .wbmp is 'wireless bitmap' and not 'windows bitmap'.
  5. return false;
  6. } else {
  7. $ext=preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename));
  8. if ($ext=='jpg') { $ext='jpeg'; }
  9. $ext='imagecreatefrom'.$ext;
  10. list($width, $height) = getimagesize($filename);
  11. $thumb = imagecreatetruecolor($newwidth, $newheight);
  12. $source = $ext($filename);
  13. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  14. return $thumb;
  15. }
  16. }
  17. $img = resize_image('file_from.gif','320','280');
  18. //save image to file
  19. imagejpeg ($img,'file.gif',100);
  20. ?>
Hope that helps ya as I would imagine that some of the previous posts would be a little confusing without the syntax highlighter.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Jun 5th, 2009
0

Re: How to resize images

I do not understand why we (the programmers) use unnecessary huge buggy codes of our own for resizing images??!! As we've phpThumb, custom image resizing functions are just a history. Then why do we manually write those codes again and make our script buggy!

Yes I know what I am saying. I was using my own codes for image resizing and one day I got problem with corrupted images and got no solution. Then I used phpThumb and the problem is fixed..
Last edited by Ezzaral; Jun 5th, 2009 at 6:32 pm. Reason: Snipped blog plug. Keep it on site please.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adnan3250 is offline Offline
1 posts
since Jun 2009
Jun 5th, 2009
0

Re: How to resize images

Click to Expand / Collapse  Quote originally posted by adnan3250 ...
I do not understand why we (the programmers) use unnecessary huge buggy codes of our own for resizing images??!! As we've phpThumb, custom image resizing functions are just a history. Then why do we manually write those codes again and make our script buggy!

Yes I know what I am saying. I was using my own codes for image resizing and one day I got problem with corrupted images and got no solution. Then I used phpThumb and the problem is fixed..

To know more details please visit this [URL snipped]
Because that's how programmers work. phpThumb didn't magically appear, there was some dumb guy/gal saying "Well I don't like that library, I'm going to write my own and name it phpThumb." It just so happens other people started using it. So, of course, there are still programmers that say "Well, I don't like that library I'm going to write my own."

Most of the time it's because in programmers' heads their code rocks and everyone else's sucks.
Last edited by Ezzaral; Jun 5th, 2009 at 6:34 pm. Reason: Snipped blog url from quoted text.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Nov 23rd, 2010
0
Re: How to resize images
Hi @cwarn23 , i want ask something if you dont mind. I had look to your example code. It simple and nice but it is useful for resizing image that will store to db as blob? I try to apply it, but not working . Hope you can give an idea based on your experience. Appreciate your respon. Thanks in advance.

just in case, here my code
PHP Syntax (Toggle Plain Text)
  1. if ($submit == 'Simpan')
  2. {
  3. global $strDesc;
  4. $max_size = 1000000000;
  5. $fileUpload = $HTTP_POST_FILES['fileUpload1']['tmp_name'] ;
  6. if($fileUpload != NULL)
  7. {
  8. $fileUpload_name=$HTTP_POST_FILES['fileUpload1']['name'];
  9. $fileUpload_size=$HTTP_POST_FILES['fileUpload1']['size'];
  10. $fileUpload_type=$HTTP_POST_FILES['fileUpload1']['type'];
  11. $fileUpload = $HTTP_POST_FILES['fileUpload1']['tmp_name'] ;
  12.  
  13. $fileHandle=fopen($fileUpload, "rb");
  14. $fileContent=fread($fileHandle,filesize($fileUpload));
  15. $fileContent=addslashes($fileContent);
  16. fclose($fileHandle);
  17.  
  18. $sqls="";
  19. if ($fileUpload_size > (1024*1024))
  20. {
  21. echo "reduce";
  22. $fileContent=resize_image($fileUpload_name,'500','500');
  23. echo $fileContent;
  24. $sqls = "INSERT INTO image_store (image_1,description) VALUES ('$fileContent','$fileUpload_type')";
  25. }
  26. else
  27. {
  28. echo "add biasa";
  29. $sqls = "INSERT INTO image_store (image_1,description) VALUES ('$fileContent','$fileUpload_type')";
  30. }
  31. echo $sqls;
  32. // if($sqls !="")
  33. // $result = mysql_query($sqls);
  34.  
  35. echo '<span style="width: 600; height: 30; font-size: 20px; font-family: verdana; color: #FF0000; ">Berjaya!!!!.</span><br>';
  36. }
  37.  
  38. }
  39. function resize_image($filename,$newwidth,$newheight)
  40. {
  41. echo $filename;
  42. if (!file_exists($filename) || !in_array(preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename)),array('png','jpg','jpeg','gif','xbm','xpm','wbmp','pjpeg')))
  43. {
  44. //note .wbmp is 'wireless bitmap' and not 'windows bitmap'.
  45. echo "false";
  46. return false;
  47. }
  48. else
  49. {
  50. echo "true";
  51. $ext=preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename));
  52. if ($ext=='jpg') { $ext='jpeg'; }
  53. $ext='imagecreatefrom'.$ext;
  54. list($width, $height) = getimagesize($filename);
  55. $thumb = imagecreatetruecolor($newwidth, $newheight);
  56. $source = $ext($filename);
  57. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  58. return $thumb;
  59. }
  60. }


Click to Expand / Collapse  Quote originally posted by cwarn23 ...
Hi, I have made a function that will resize images and is as follows:
php Syntax (Toggle Plain Text)
  1. <?php
  2. function resize_image($filename,$newwidth,$newheight) {
  3. if (!file_exists($filename) || !in_array(preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename)),array('png','jpg','jpeg','gif','xbm','xpm','wbmp'))) {
  4. //note .wbmp is 'wireless bitmap' and not 'windows bitmap'.
  5. return false;
  6. } else {
  7. $ext=preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename));
  8. if ($ext=='jpg') { $ext='jpeg'; }
  9. $ext='imagecreatefrom'.$ext;
  10. list($width, $height) = getimagesize($filename);
  11. $thumb = imagecreatetruecolor($newwidth, $newheight);
  12. $source = $ext($filename);
  13. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  14. return $thumb;
  15. }
  16. }
  17. $img = resize_image('file_from.gif','320','280');
  18. //save image to file
  19. imagejpeg ($img,'file.gif',100);
  20. ?>
Hope that helps ya as I would imagine that some of the previous posts would be a little confusing without the syntax highlighter.
Last edited by red_ruewei; Nov 23rd, 2010 at 3:14 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
red_ruewei is offline Offline
37 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: I want to take printout by a button of php output
Next Thread in PHP Forum Timeline: PHP read text file and save into databse





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC