Jump to content

Cropping a square from an image.


justcrapx

Recommended Posts

  • 3 months later...
Woo... I believe I have it figured out!

Here is a modified code from lixlpixel of PHP Paradise.  This will create a cropped thumbnail of a JPEG and upload them both into a specified directory.

[code]<?php

if(isset($_POST['Submit']))

{
$size = 100; // the thumbnail height

$filedir = 'pics/'; // the directory for the original image
$thumbdir = 'pics/'; // the directory for the thumbnail image
$prefix = 'THUM_'; // the prefix to be added to the original name

$maxfile = '2000000';
$mode = '0666';

$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];

if (isset($_FILES['image']['name']))
{
$prod_img = $filedir.$userfile_name;

$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
chmod ($prod_img, octdec($mode));

$sizes = getimagesize($prod_img);

$aspect_ratio_wide = $sizes[1]/$sizes[0];
$aspect_ratio_tall = $sizes[0]/$sizes[1];

if ($sizes[1] > $sizes[0]) // if height is greater than width
{

if ($sizes[0] <= $size) // if width <= defined size
{
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_width = $size;
$new_height = abs($new_width/$aspect_ratio_tall); }

$size_difference_height = $sizes[1] / $new_height;
$center_width = 0;
$center_height = (($new_height - $size) / 2) * $size_difference_height;

} else if ($sizes[0] > $sizes[1]) { // else if width is greater than height

if ($sizes[1] <= $size)
{
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_height = $size;
$new_width = abs($new_height/$aspect_ratio_wide);
}

$size_difference_width = $sizes[0] / $new_width;
$center_width = (($new_width - $size) / 2) * $size_difference_width;
$center_height = 0;

}


$destimg = imagecreatetruecolor($size,$size)
or die('Problem In Creating image');
$srcimg = imagecreatefromjpeg($prod_img)
or die('Problem In opening Source Image');

imagecopyresampled($destimg,$srcimg,0,0,$center_width,$center_height,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
or die('Problem In resizing');

ImageJPEG($destimg,$prod_img_thumb,90)
or die('Problem In saving');
imagedestroy($destimg);
}

echo '
<a href="'.$prod_img.'">
<img src="'.$prod_img_thumb.'" width="$size" height="$size">
</a>
';


}else{

echo '
<form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
<input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.