Advertisement
gundambison

resize gambar

Mar 8th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. <?php
  2.  
  3. $file_gbr='1.jpg'; //contoh memakai jpg silakan update dengan keperluan
  4. $file_hasil='1_res.jpg';
  5. $width=300;
  6. $height=500;
  7. $center=TRUE;
  8.  
  9. ResizeImage($file_gbr, $file_hasil, $width, $height, $center);
  10.  
  11. echo "<img src='".$file_hasil."' />";
  12.  
  13. function resizeImage($img0, $img1, $width1, $height1, $center=FALSE){
  14.     list($width0, $height0) = getimagesize($img0);
  15.     //sumber https://stackoverflow.com/questions/14649645/resize-image-in-php
  16.     //$r = $width / $height;
  17.     //perbandingan antara panjang lebar gambar sumber
  18.     $resize0 = $width0 / $height0;
  19.    
  20.     //perbandingan antara panjang lebar gambar tujuan
  21.     $resize1 = $width1 / $height1;
  22.     logTxt("r: $resize0 | $resize1 ");
  23.     if( $resize1 > $resize0){
  24.         logTxt('-r1-');
  25.         $newWidth = $height1 * $resize0 * $resize1;
  26.         $newHeight= $height1;
  27.     }else{
  28.         logTxt('-r0-');
  29.         $newHeight = $width1 / $resize0 * $resize1;
  30.         $newWidth= $width1;
  31.     }
  32.    
  33.     logTxt("h,w: ($newHeight|$newWidth)");
  34.     //sesuaikan dengan tipe gbr
  35.     $imgSrc = imagecreatefromjpeg($img0);
  36.        
  37.     if($center){
  38.         //posisi x,y
  39.         $x1= ceil( ($width1 - $newWidth) /2);
  40.         $y1= ceil( ($height1  - $newHeight) /2);
  41.         logTxt("w,h : $width1,$height1 | $newWidth,$newHeight");
  42.         logTxt("x,y : $x1,$y1");
  43.         //ukuran disesuaikan dgn request
  44.         $imgTarget=imagecreatetruecolor($width1, $height1);
  45.         imagecopyresized($imgTarget, $imgSrc, $x1, $y1, 0, 0, $newWidth, $newHeight, $width0, $height0);
  46.        
  47.     }else{
  48.         //ukuran sesuai dengan ukuran baru
  49.         $imgTarget=imagecreatetruecolor($newWidth, $newHeight);
  50.         logTxt("not center :  $newWidth, $newHeight, $width0, $height0");
  51.         imagecopyresized($imgTarget, $imgSrc, 0, 0, 0, 0, ceil($newWidth), ceil($newHeight), $width0, $height0);
  52.        
  53.     }
  54.     //save gambar
  55.     imagejpeg($imgTarget, $img1);
  56. }
  57.  
  58. function logTxt($str){
  59.     $file='1.log';
  60.     error_log($str."\n",3,$file);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement