Crop Image from Center PHP

Crop Image From Center PHP

GD comes bundled with all PHP installations from version 4.3.6 onwards so chances are, you have it.

Here's the steps you need to take...

  1. Create an image resource using one of the GD imagecreatefrom*() functions. The one you use depends on the type of image you're dealing with
  2. Determine the image dimensions using imagesx() and imagesy()
  3. Determine your crop coordinates using the following algorithm and crop using imagecopy()

Find crop coordinates

$width  = imagesx($img);
$height = imagesy($img);
$centreX = round($width / 2);
$centreY = round($height / 2);

$cropWidth = 200;
$cropHeight = 130;
$cropWidthHalf = round($cropWidth / 2); // could hard-code this but I'm keeping it flexible
$cropHeightHalf = round($cropHeight / 2);

$x1 = max(0, $centreX - $cropWidthHalf);
$y1 = max(0, $centreY - $cropHeightHalf);

$x2 = min($width, $centreX + $cropWidthHalf);
$y2 = min($height, $centreY + $cropHeightHalf);

Somebody implemented a handy class that encapsulates this logic here ~ ImageManipulator.php

Crop from the center and resize in PHP (function)

It worked for me. You can try:

$x=288; $y=202; // my final thumb
$ratio_thumb=$x/$y; // ratio thumb

list($xx, $yy) = getimagesize($image); // original size
$ratio_original=$xx/$yy; // ratio original

if ($ratio_original>=$ratio_thumb) {
$yo=$yy;
$xo=ceil(($yo*$x)/$y);
$xo_ini=ceil(($xx-$xo)/2);
$xy_ini=0;
} else {
$xo=$xx;
$yo=ceil(($xo*$y)/$x);
$xy_ini=ceil(($yy-$yo)/2);
$xo_ini=0;
}

imagecopyresampled($thumb, $source, 0, 0, $xo_ini, $xy_ini, $x, $y, $xo, $yo);

Cropping an image from the center into the largest square you can make in Yii2 Imagine?

Another try :p

<?php

use yii\imagine\Image;
use Imagine\Image\Box;
use Imagine\Image\Point;

// ...

$thumbnail = Image::thumbnail($save_path, $img_size, $img_size);
$size = $thumbnail->getSize();
if ($size->getWidth() < $img_size or $size->getHeight() < $img_size) {
$white = Image::getImagine()->create(new Box($img_size, $img_size));
$thumbnail = $white->paste($thumbnail, new Point($img_size / 2 - $size->getWidth() / 2, $img_size / 2 - $size->getHeight() / 2));
}
$thumbnail->save($save_path);

Crop center square of image using imagecopyresampled

I think you are going wrong in your thumb1_tmp_img near the end. I made this a class so I didn't have to duplicate so much like you have, but you can see the changes. See if this does it for you:

class ImageFactory
{
public function MakeThumb($thumb_target = '', $width = 60,$height = 60,$SetFileName = false, $quality = 80)
{
$thumb_img = imagecreatefromjpeg($thumb_target);

// size from
list($w, $h) = getimagesize($thumb_target);

if($w > $h) {
$new_height = $height;
$new_width = floor($w * ($new_height / $h));
$crop_x = ceil(($w - $h) / 2);
$crop_y = 0;
}
else {
$new_width = $width;
$new_height = floor( $h * ( $new_width / $w ));
$crop_x = 0;
$crop_y = ceil(($h - $w) / 2);
}

// I think this is where you are mainly going wrong
$tmp_img = imagecreatetruecolor($width,$height);

imagecopyresampled($tmp_img, $thumb_img, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $w, $h);

if($SetFileName == false) {
header('Content-Type: image/jpeg');
imagejpeg($tmp_img);
}
else
imagejpeg($tmp_img,$SetFileName,$quality);

imagedestroy($tmp_img);
}
}

// Initiate class
$ImageMaker = new ImageFactory();

// Here is just a test landscape sized image
$thumb_target = 'http://media1.santabanta.com/full6/Outdoors/Landscapes/landscapes-246a.jpg';

// 60px x 60px
$ImageMaker->MakeThumb($thumb_target,60,60,'image60px.jpg');

// Here is a portrait for testing
$thumb_target = 'http://imgc.allpostersimages.com/images/P-488-488-90/55/5543/L6HLG00Z/posters/warning-you-are-entering-a-fart-zone.jpg';

// 150px x 150px
$ImageMaker->MakeThumb($thumb_target,150,150,'image150px.jpg');

PHP crop image to fix width and height without losing dimension ratio

This is done by only using a part of the image as the thumbnail which has a 1:1 aspect ratio (mostly the center of the image). If you look closely you can see it in the flickr thumbnail.

Because you have "crop" in your question, I'm not sure if you didn't already know this, but what do you want to know then?

To use cropping, here is an example:

//Your Image
$imgSrc = "image.jpg";

//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);

//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);

// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}

// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);

PHP Imagick - Center Cropped image on top of another

If you only want a transparent background you don't need a separate image file. Just crop the image and resize it.

<?php
header('Content-type: image/png');

$path = 'image.jpg';
$image = new Imagick($path);
$geometry = $image->getImageGeometry();

$width = $geometry['width'];
$height = $geometry['height'];

$crop_width = 225;
$crop_height = 430;
$crop_x = ($width - $crop_width) / 2;
$crop_y = ($height - $crop_height) / 2;

$size = 500;

$image->cropImage($crop_width, $crop_height, $crop_x, $crop_y);
$image->setImageFormat('png');
$image->setImageBackgroundColor(new ImagickPixel('transparent'));
$image->extentImage($size, $size, -($size - $crop_width) / 2, -($size - $crop_height) / 2);

echo $image;

Use setImageFormat to convert the image to PNG (to allow transparency), then set a transparent background with setImageBackgroundColor. Finally, use extentImage to resize it.

Crop image in PHP

If you are trying to generate thumbnails, you must first resize the image using imagecopyresampled();. You must resize the image so that the size of the smaller side of the image is equal to the corresponding side of the thumb.

For example, if your source image is 1280x800px and your thumb is 200x150px, you must resize your image to 240x150px and then crop it to 200x150px. This is so that the aspect ratio of the image won't change.

Here's a general formula for creating thumbnails:

$image = imagecreatefromjpeg($_GET['src']);
$filename = 'images/cropped_whatever.jpg';

$thumb_width = 200;
$thumb_height = 150;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);

Haven't tested this but it should work.

EDIT

Now tested and working.



Related Topics



Leave a reply



Submit