Skip to content

Instantly share code, notes, and snippets.

@enlacee
Created April 24, 2019 04:34
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 enlacee/fc175011a0aff6bb3e679f57e070aca5 to your computer and use it in GitHub Desktop.
Save enlacee/fc175011a0aff6bb3e679f57e070aca5 to your computer and use it in GitHub Desktop.
Crear imagen proporcional con Imagick LIBRARY
<?php
/*
// BASIC EXAMPLE (INSPIRATION)
// comando usado en LINUX con imagemagick instalado
// convert sansung.png -thumbnail 500x500 -background black -gravity center -extent 500x500 end.jpg
header('Content-type: image/jpeg');
$image = new Imagick('image.jpg');
$image->setImageBackgroundColor('black');
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage(500, 500,true);
$ancho = $image->getImageWidth();
$alto = $image->getImageHeight();
// $image->scaleImage(500, 500, true);
$image->setGravity(imagick::GRAVITY_CENTER);
$image->extentImage(500,500,0,0);
// $image->extentImage(500,500,0,-109.5);
// $image->adaptiveResizeImage(500,500);
echo $image;
*/
header('Content-type: image/jpeg');
$setAncho = 500;
$setAlto = 500;
$image = new Imagick('image.jpg');
$image->setImageBackgroundColor('black');
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage($setAncho, $setAlto,true);
// ancho o alto variable
$Currentancho = $image->getImageWidth();
$Currentalto = $image->getImageHeight();
$newX = 0;
$newY = 0;
if ($Currentancho < $setAncho ) {
$newX = (($Currentancho - $setAncho) / 2);
}
if ($Currentalto < $setAlto ) {
$newY = (($Currentalto - $setAlto) / 2);
}
// echo $newX;
// var_dump($newX);
// echo "<br>";
// var_dump($newY);EXIT;
$image->setGravity(imagick::GRAVITY_CENTER);
$image->extentImage($setAncho,$setAlto,$newX,$newY);
echo $image;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment