Skip to content

Instantly share code, notes, and snippets.

@melissacabral
Created February 12, 2018 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melissacabral/e0f9a1fd5da852050d93889f9edfa806 to your computer and use it in GitHub Desktop.
Save melissacabral/e0f9a1fd5da852050d93889f9edfa806 to your computer and use it in GitHub Desktop.
Change the image uploader we built so that it preserves the original aspect ratio. Replaces lines in the foreach($sizes) loop. Prevents up-sampling
if($width >= $pixels){
//wider than desired size
$newwidth = $pixels;
//calculate the correct height
$newheight=($height/$width) * $newwidth;
}else{
//smaller than desired size. preserve original width and height
$newwidth=$width;
$newheight=$height;
}
$tmp_canvas = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp_canvas,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment