Examples of how to scale up or down php images
/**
- * Proportional scaling up or down of images
- * @ Resizeimage
- * @ Param $ srcfile source file size
- * @ Param $ mySize the size to be converted
- * Edit bbs.it-home.org
- * At 2013/6/7
- */
- Function resizeimage ($ srcfile, $ mySize ){
- $ Size = getimagesize ($ srcfile );
- Switch ($ size [2]) {
- Case 1:
- $ Img = imagecreatefromgif ($ srcfile );
- Break;
- Case 2:
- $ Img = imagecreatefromjpeg ($ srcfile );
- Break;
- Case 3:
- $ Img = imagecreatefrompng ($ srcfile );
- Break;
- }
- // Width and height of the source image
- $ OldImg ['w'] = imagesx ($ img );
- $ OldImg ['H'] = imagesy ($ img );
- If ($ oldImg ['w'] <= $ mySize ['w'] & $ oldImg ['H'] <156 ){
- $ Rate = 1;
- } Elseif ($ oldImg ['w']> $ mySize ['w'] & $ oldImg ['H'] <$ mySize ['H']) {
- $ Rate = $ mySize ['w']/$ oldImg ['w'];
- } Elseif ($ oldImg ['w'] <$ mySize ['w'] & $ oldImg ['H']> $ mySize ['H']) {
- $ Rate = $ mySize ['H']/$ oldImg ['H'];
- } Elseif ($ oldImg ['w']> $ mySize ['w'] & $ oldImg ['H']> $ mySize ['H']) {
- $ Rate1 = $ mySize ['w']/$ oldImg ['w'];
- $ Rate2 = $ mySize ['H']/$ oldImg ['H'];
- If ($ rate1 >$ rate2) {$ rate = $ rate2;} else {$ rate = $ rate1 ;}
- }
- $ NewImg ['w'] = $ oldImg ['w'] * $ rate;
- $ NewImg ['H'] = $ oldImg ['H'] * $ rate;
- Return "width =". $ newImg ['w']. "height =". $ newImg ['H'];
- }
// Call example:
- $ MySize = array ('W' => 143, 'H' => 156 );
- $ ImgSize = resizeimage ("22.jpg", $ mySize );
- Echo "";
- ?>
|