html - How to resize image keeping constraints php -


i have made 2 gifs explain trying do. grey border dimensions after (700*525). @ bottom of question.

i want images larger given width , height scale down border (from centre) , crop off edges. here code have put attempt this:

if ($heightofimage => 700 && $widthofimage => 525){     if ($heightofimage > $widthofimage){          $widthofimage = 525;         $heightofimage = //scaled height.          //crop height 700.      }      if ($heightofimage < $widthofimage){          $widthofimage = //scaled width.         $heightofimage = 700;          //crop width 525.      } }else{     echo "image small"; } 

here gifs visually explain trying achieve:

gif 1: here image proportions in x direction

enter image description here

gif 2: here image proportions in y direction

enter image description here


image quality comparison @timclutton

so have used method php (click here own test php) , compared original photo can see there big difference!:

your php method:

http://tragicclothing.co.uk/video%20images/test/yo.jpg

the actual file:

enter image description here


the below code should want. i've not tested extensively seems work on few test images made. there's niggling doubt @ of mind somewhere math wrong, it's late , can't see obvious.

edit: niggled enough went through again , found bug, crop wasn't in middle of image. code replaced working version.

in short: treat starting point, not production-ready code!

<?php  // set image size constraints. $target_w = 525; $target_h = 700;  // image. $in = imagecreatefrompng('<path your>.png');  // image dimensions. $w = imagesx($in); $h = imagesy($in);  if ($w >= $target_w && $h >= $target_h) {     // scales.     $x_scale = ($w / $target_w);     $y_scale = ($h / $target_h);      // create new image.     $out = imagecreatetruecolor($target_w, $target_h);      $new_w = $target_w;     $new_h = $target_h;     $src_x = 0;     $src_y = 0;      // compare scales ensure crop whichever smaller: top/bottom or     // left/right.     if ($x_scale > $y_scale) {         $new_w = $w / $y_scale;          // see description of $src_y, below.         $src_x = (($new_w - $target_w) / 2) * $y_scale;     } else {         $new_h = $h / $x_scale;          // bit tricky. crop done specifying coordinates copy in         // source image. calculate how remove new image ,         // scale original. result out ~1px enough.         $src_y = (($new_h - $target_h) / 2) * $x_scale;     }      // given right inputs, takes care of crop , resize , gives     // new image. note imagecopyresized() possibly quicker,     // imagecopyresampled() gives better quality.     imagecopyresampled($out, $in, 0, 0, $src_x, $src_y, $new_w, $new_h, $w, $h);      // output browser.     header('content-type: image/png');     imagepng($out);     exit;  } else {     echo 'image small'; }  ?> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -