<?
The code is as follows: |
Copy code |
$ W? $ RESIZEWIDTH = $ w: $ RESIZEWIDTH = 400; // The width of the generated image. $ H? $ RESIZEHEIGHT = $ h: $ RESIZEHEIGHT = 400; // The height of the generated image. Function ResizeImage ($ im, $ maxwidth, $ maxheight, $ name ){ $ Width = imagesx ($ im ); $ Height = imagesy ($ im ); If ($ maxwidth & $ width> $ maxwidth) | ($ maxheight & $ height> $ maxheight )){ If ($ maxwidth & $ width> $ maxwidth ){ $ Widthratio = $ maxwidth/$ width; $ RESIZEWIDTH = true; // www.111cn.net } If ($ maxheight & $ height> $ maxheight ){ $ Heightratio = $ maxheight/$ height; $ RESIZEHEIGHT = true; } If ($ RESIZEWIDTH & $ RESIZEHEIGHT ){ If ($ widthratio <$ heightratio ){ $ Ratio = $ widthratio; } Else { $ Ratio = $ heightratio; } } Elseif ($ RESIZEWIDTH ){ $ Ratio = $ widthratio; } Elseif ($ RESIZEHEIGHT ){ $ Ratio = $ heightratio; } $ Newwidth = $ width * $ ratio; $ Newheight = $ height * $ ratio; If (function_exists ("imagecopyresampled ")){ $ Newim = imagecreatetruecolor ($ newwidth, $ newheight ); Imagecopyresampled ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height ); } Else { $ Newim = imagecreate ($ newwidth, $ newheight ); Imagecopyresized ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height ); } ImageJpeg ($ newim, $ name ); ImageDestroy ($ newim ); } Else { ImageJpeg ($ im, $ name ); } } |
The code is as follows: |
Copy code |
If ($ _ FILES ['uploadfile'] ['size']) { If ($ _ FILES ['uploadfile'] ['type'] = "image/pjpeg "){ $ Im = imagecreatefromjpeg ($ _ FILES ['uploadfile'] ['tmp _ name']); } Elseif ($ _ FILES ['uploadfile'] ['type'] = "image/x-png "){ $ Im = imagecreatefrompng ($ _ FILES ['uploadfile'] ['tmp _ name']); } Elseif ($ _ FILES ['uploadfile'] ['type'] = "image/gif "){ $ Im = imagecreatefromgif ($ _ FILES ['uploadfile'] ['tmp _ name']); } If ($ im ){ If(file_exists('bbs.jpg ')){ Unlink('www.111cn.net.jpg '); } Resizeimage(%im,%resizewidth,%resizeheight,'bbs.jpg '); ImageDestroy ($ im ); } } // $ Uploadfile = "bbs.jpg ";
|
?>
Create imageRemember to use ImageDestroy to clear the memory.