/    Sign up×
Community /Pin to ProfileBookmark

Crop tall .jpg to square WITHOUT complex cropping add-ons

I have a handful of jpg files that are tall and vary in size. I want the excess length to be trimmed (so aligned top-center and cropped to a square) but I don’t want to add any extra systems like thumb.php, ImageMagick, etc. to the page because simplicity and file-size are important for this quick project.

They are [I]all[/I] 430 pixels wide and they should [I]all[/I] be aligned to the top (rather than cropped to the center or something)

Any advice or help would be greatly appreciated.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 26.2013 — Here's a stripped down version of a function I wrote a few years ago that should work (if I didn't break it):
[code=php]
/**
* Resize image to specific dimension, cropping as needed
* @return resource Resized image resource, or boolean false on failure
* @param string $imgFile Path to image to be resized
* @param int $width
* @param int $height
* @param string $error Error message
*/
function myCrop($imgFile, $width=430, $height=430, &$error = null)
{
$attrs = @getimagesize($imgFile);
if($attrs == false or $attrs[2] != IMG_JPEG)
{
$error = "Uploaded image is not JPEG or is not readable by this page.";
return false;
}
$resized = imagecreatetruecolor($width, $height);
$result = imagecopyresampled($resized, $src, 0, 0, 0, 0, $width, $height, $width, $height);
if($result == false)
{
$error = "Error trying to resize and crop image.";
return false;
}
else
{
return $resized;
}
}
[/code]

Usage would be:
[code=php]
$resizedImage = myCrop(imagecreatefromjpeg('path/to/your/image.jpg'));
// now do whatever you want with the image, such as saving it as a file:
imagejpeg($resizedImage, 'path/to/resized/image.jpg', 90);
[/code]
Copy linkTweet thisAlerts:
@amandaNHTauthorSep 26.2013 — I'm trying it but it isn't working. ?
Copy linkTweet thisAlerts:
@NogDogSep 26.2013 — Looks like I screwed up a couple things when stripping out the features you didn't need. This test worked fine for me. (Note that it's outputting the image directly, versus writing it to a file. See my first reply for the imagejpeg() syntax to write it to a file if that's what you need.)
[code=php]
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$imageFile = 'C:UsersPublicPicturesSample PicturesPenguins.jpg';
$newImage = myCrop($imageFile, 430, 430, $error);
if($newImage == false) {
die($error);
}

// this is directly displaying the image, change it to write to file if desired:
header('Content-Type: image/jpeg');
imagejpeg($newImage);
exit;

/**
* Resize image to specific dimension, cropping as needed
* @return resource Resized image resource, or boolean false on failure
* @param string $imgFile Path to image to be resized
* @param int $width
* @param int $height
* @param string $error Error message
*/
function myCrop($imgFile, $width=430, $height=430, &$error = null)
{
$attrs = @getimagesize($imgFile);
if($attrs == false) // or $attrs[2] != IMG_JPEG)
{
$error = "Uploaded image is not JPEG or is not readable by this page.";
return false;
}
$src = imagecreatefromjpeg($imgFile);
if($src == false) {
$error = "Unable to load source image.";
return false;
}
$resized = imagecreatetruecolor($width, $height);
$result = imagecopyresampled($resized, $src, 0, 0, 0, 0, $width, $height, $width, $height);
if($result == false)
{
$error = "Error trying to resize and crop image.";
return false;
}
else
{
return $resized;
}
}
[/code]
×

Success!

Help @amandaNHT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...