login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > GD library > GD Image resize functie

GD Image resize functie

Auteur: Koen - 01 januari 2009 - 13:43 - Gekeurd door: Stijn - Hits: 8445 - Aantal punten: 5.00 (6 stemmen)



Uitleg

Met deze functie kan je gemakkelijk een afbeelding resizen zonder dat deze uitgerokken wordt.
Vergeet niet om het bestand voldoende chmod rechten te geven!

Veel plezier 

Code:
  1. <?php
  2. /**
  3. * ResizeImage
  4. * @desc A function to easily resize images
  5. *
  6. * @author Koen <koen@sitemasters.be>
  7. * @version 1.2
  8. *
  9. * @param string $sImage The image you would like to have resized
  10. * @param int $iMaxWidth The maximum width [Optional, default: 600]
  11. * @param int $iMaxHeight The maximum height [Optional, default: 600]
  12. *
  13. * @return False on failure, Array(name, mime, width, height) on succeed.
  14. *
  15. *
  16. * Changelog:
  17. * 10-01-2009
  18. * Added support for "image/pjepg" mime type. (Internet Explorer uses this.)
  19. * 24-01-2009
  20. * Fastened script.
  21. */
  22.  
  23. function ResizeImage($sImage, $iMaxWidth=600, $iMaxHeight=600)
  24. {
  25. if($aSize = @getimagesize($sImage))
  26. {
  27. list($iOrigWidth, $iOrigHeight) = $aSize;
  28. $sMimeType = $aSize['mime'];
  29. $rResized = null;
  30. switch($sMimeType)
  31. {
  32. case 'image/jpeg':
  33. case 'image/pjpeg':
  34. case 'image/jpg':
  35. $rResized = imagecreatefromjpeg($sImage);
  36. break;
  37. case 'image/gif':
  38. $rResized = imagecreatefromgif($sImage);
  39. break;
  40. case 'image/png':
  41. case 'image/x-png':
  42. $rResized = imagecreatefrompng($sImage);
  43. break;
  44. default:
  45. return false;
  46. }
  47. if(isset($iOrigWidth, $iOrigHeight))
  48. {
  49. if($iOrigWidth <= $iMaxWidth && $iOrigHeight <= $iMaxHeight)
  50. {
  51. $iNewWidth = $iOrigWidth;
  52. $iNewHeight = $iOrigHeight;
  53. } else
  54. {
  55. $iOrigRatio = $iOrigWidth / $iOrigHeight;
  56. if(($iMaxWidth/$iMaxHeight) > $iOrigRatio)
  57. {
  58. $iNewWidth = $iMaxHeight * $iOrigRatio;
  59. $iNewHeight = $iMaxHeight;
  60. } else
  61. {
  62. $iNewHeight = $iMaxWidth / $iOrigRatio;
  63. $iNewWidth = $iMaxWidth;
  64. }
  65. }
  66. $rResampledImage = imagecreatetruecolor($iNewWidth, $iNewHeight);
  67. imagecopyresampled($rResampledImage, $rResized, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOrigWidth, $iOrigHeight);
  68. unlink($sImage);
  69. switch($sMimeType)
  70. {
  71. case 'image/jpeg':
  72. case 'image/pjpeg':
  73. case 'image/jpg':
  74. imagejpeg($rResampledImage, $sImage, 100);
  75. break;
  76. case 'image/gif':
  77. imagegif($rResampledImage, $sImage);
  78. break;
  79. case 'image/png':
  80. case 'image/x-png':
  81. imagepng($rResampledImage, $sImage);
  82. break;
  83. default:
  84. return false;
  85. }
  86. @chmod($sImage, 0777);
  87. return array( "name" => $sImage,
  88. "mime" => $sMimeType,
  89. "width" => $iNewWidth,
  90. "height" => $iNewHeight
  91. );
  92. } else
  93. {
  94. return false;
  95. }
  96. } else
  97. {
  98. return false;
  99. }
  100. }
  101.  
  102. // Voorbeeld:
  103.  
  104. echo '<pre>', print_r(resize('picture.png')), '</pre>';
  105.  
  106. // Return Value:
  107.  
  108. (
  109. [name] => picture.png
  110. [mime] => image/png
  111. [width] => 436
  112. [height] => 600
  113. )
  114.  
  115. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.025s