Skip to content

Instantly share code, notes, and snippets.

@LiamKarlMitchell
Created October 31, 2018 06:40
Show Gist options
  • Save LiamKarlMitchell/a49ca39ee7b4f1fe332643c653ecd058 to your computer and use it in GitHub Desktop.
Save LiamKarlMitchell/a49ca39ee7b4f1fe332643c653ecd058 to your computer and use it in GitHub Desktop.
php create square image resized to best fit
<?php
// Note: Has problem with color spaces if input image is not sRGB some color info will be dropped. Loss of saturation/vibrance may be noticable.
function create_square_image($original_file, $destination_file=NULL, $square_size = 96){
// get width and height of original image
$imagedata = getimagesize($original_file);
$original_width = $imagedata[0];
$original_height = $imagedata[1];
if($original_width > $original_height){
$new_height = $square_size;
$new_width = $new_height*($original_width/$original_height);
}
if($original_height > $original_width){
$new_width = $square_size;
$new_height = $new_width*($original_height/$original_width);
}
if($original_height == $original_width){
$new_width = $square_size;
$new_height = $square_size;
}
$new_width = round($new_width);
$new_height = round($new_height);
// load the image
if(substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")){
$original_image = imagecreatefromjpeg($original_file);
}
if(substr_count(strtolower($original_file), ".gif")){
$original_image = imagecreatefromgif($original_file);
}
if(substr_count(strtolower($original_file), ".png")){
$original_image = imagecreatefrompng($original_file);
}
$smaller_image = imagecreatetruecolor($new_width, $new_height);
$square_image = imagecreatetruecolor($square_size, $square_size);
imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
if($new_width>$new_height){
$difference = $new_width-$new_height;
$half_difference = round($difference/2);
imagecopyresampled($square_image, $smaller_image, 0-$half_difference+1, 0, 0, 0, $square_size+$difference, $square_size, $new_width, $new_height);
}
if($new_height>$new_width){
$difference = $new_height-$new_width;
$half_difference = round($difference/2);
imagecopyresampled($square_image, $smaller_image, 0, 0-$half_difference+1, 0, 0, $square_size, $square_size+$difference, $new_width, $new_height);
}
if($new_height == $new_width){
imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
}
//// if no destination file was given then display a png
//if(!$destination_file){
// imagepng($square_image,NULL,9);
//}
// save the smaller image FILE if destination file given
if(substr_count(strtolower($destination_file), ".jpg")){
imagejpeg($square_image,$destination_file,100);
}
if(substr_count(strtolower($destination_file), ".gif")){
imagegif($square_image,$destination_file);
}
if(substr_count(strtolower($destination_file), ".png")){
imagepng($square_image,$destination_file,9);
}
imagedestroy($original_image);
imagedestroy($smaller_image);
imagedestroy($square_image);
}
<?php
// Note: If you are trying to manipulate a uploaded file and then save the file all in the same request with Apache + mod_dav this will fail.
function create_square_image($original_file, $destination_file=NULL, $square_size = 96){
// Over-write original.
if (!$destination_file) {
$destination_file = $original_file;
}
$im = new Imagick($original_file);
// Ensure image colorspace is SRGB.
if ($im->getColorspace() !== imagick::COLORSPACE_SRGB) {
$im->transformImageColorspace(imagick::COLORSPACE_SRGB);
}
// Remove pixels that are the background color/within fuzzy selection. // Note: The tolerance here was too high, so I just commented it out, results may vary.
//$im->trimImage(20000); // Quantum Range is probably 65535 can get with $im->getQuantumRange() which returns an associative array.
// Resize image to a square size with best fit.
$im->resizeImage($square_size, $square_size, Imagick::FILTER_LANCZOS, 1, TRUE);
$im->setImageBackgroundColor("white");
// Decide how to crop this image to the appropriate square size.
$w = $im->getImageWidth();
$h = $im->getImageHeight();
$off_top=0;
$off_left=0;
if ($w > $h){
$off_top = (($square_size-$h)/2) * -1;
} else {
$off_left = (($square_size-$w)/2) * -1;
}
$im->extentImage($square_size, $square_size, $off_left, $off_top);
$im->writeImage($destination_file);
}
@LiamKarlMitchell
Copy link
Author

To Install PHP Imagemagick.

apt-get install php-imagick

Verify it loaded as an extension.

php -m | grep imagick

Restart Apache

service apache2 restart

To put image magick for php 7.2 into a docker container.

sudo apt-get update && sudo apt-get install -y --no-install-recommends libmagickwand-dev && sudo rm -rf /var/lib/apt/lists/* && sudo pecl install imagick-3.4.3 && sudo docker-php-ext-enable imagick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment