Coding


7
Feb 11

Square Crop

As part of the work I’ve been doing on CraftNation I’ve been looking into image cropping and resizing. I’m sorry to say that due to a lack of affordable photo manipulation software1 we often get images which have come straight out of digital camera, and are huge, about 3meg each. And with the facility to upload 4 images per deal, this can ( and has ) maxed out PHP’s maximum upload size which is 8meg. In addition images from the camera are not square, which is a bit of a pain when you are displaying square images on your site, and also in specific dimensions. The typical aspect ratio for your images shot on a digital camera are 4:3 or 1.33 the same aspect ratio of the pre-widescreen televisions.

This means that the image is 1.33 times wider than it is high. And if you’ve been cropping out the corner of the airing horse which is just in shot then the aspect ratio could be anything.

Here we have a pale pink rectangle which is in the 4:3 ratio and centred over it is a darker square image.

Now simply resizing a wide (or tall) image to be square will result in the image being squashed, or stretched in one direction.

For the last while we’ve been troubled with how to make it easier for CraftNation users to deal with their images. We’ve toyed with a cropping feature on the upload form but to be honest this isn’t practical, they are on the site to upload their deals not do image manipulation.

So instead I’ve been working on some functionality that will2 go on behind the scenes after the images have been uploaded.

The first stage involves resizing the image so that the shortest side is the same length as that of our largest square image (in this case 350px) – this is because we want to resize as well as crop – so we make sure that we retain as much of the image as possible.

The second stage then crops the resized image so that it is a square, the code I’ve written crops from centre. If you think of your image as the pink one above, the code creates a square image which contains whatever is in the grey block above.

I used imagecopyresampled() to make sure that the image quality is kept as good as possible.

[1] There are a number of good web based photo manipulation sites such as Picnik which are a decent alternative to Adobe Photoshop and allow you to resize, crop and play around with the white balance and exposure.

[2] This feature has not yet been implemented on the site.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// initialise to settings for a square image
$new_w = $e;
$new_h = $e;
$x_offset = 0;
$y_offset = 0;
// wide image
if($h < $w){
  $new_w = ($w * $e)/$h;
  $x_offset = ($new_w/2) - ($e/2);
}
// tall image
else if($w < $h){
  $new_h = ($h * $e)/$w;
  $y_offset = ($new_h/2) - ($e/2);
}
 
$orig_image = '';
switch($imageType) {
  case "image/gif":
    $orig_image=imagecreatefromgif($img);
    break;
 
  case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
    $orig_image=imagecreatefromjpeg($img);
    break;
 
  case "image/png":
  case "image/x-png":
    $orig_image=imagecreatefrompng($img);
    break;
}
 
// images
$resized_img = imagecreatetruecolor($new_w, $new_h);
$cropped_img = imagecreatetruecolor($e, $e);
 
imagecopyresampled($resized_img, $orig_image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
imagecopyresampled($cropped_img, $resized_img, 0, 0, $x_offset, $y_offset, $e, $e, $e, $e);
 
//output the image to the browser
header('Content-type: image/jpeg');
imagejpeg($cropped_img, null, 100);
?>

Test image – set to 350px wide, note the height is less than 350

Image forced to be square

When we set the width to 350px above the height was less than 350px. By forcing the image to be 350px square  the height has been pulled to make it 350 as well, resulting in a horizontal squash and vertical stretch and a very unimpressed Amelia.

This is how it should look not squashed nor stretched but a nice centred crop.

Square H/W in pixels:

Full web address of image:


17
Dec 10

CraftNation.com

I’ve been somewhat more than usually quiet on the blogging front, but the lack of clamouring for posts would suggest it hasn’t been missed. The reason for this, besides having a 2 and a half year old running, and I mean running about the place is that I’ve been spending every spare moment working on the CraftNation.com website.

Back in June/July I did some work with Louise of MyEhive to build on her CraftNationsUnite google map where designer makers could add their marker on to the map. This led to a database driven version of the map where designers could create a profile page in addition to contributing to the map.

Now it features accounts for non designers providing them with a private map showing them local designers.

Designers are invited to submit their best products to a voting showcase limited to 25 spots which runs from Monday to Monday and users vote on which are to be the special deals for the following week, enabling makers to make only want is wanted cutting down on waste.

You can see the current showcase here http://www.craftnation.com/handmade-artisan-craft-gifts/vote/

At the moment there are plenty of places still available for next week’s showcase so if you are a designer why not stop by, create your profile and submit a deal?

The work on the site is by no means complete and I’ll be continuing my work on it in the New Year.

You can explore the map here: http://www.craftnation.com/handmade-artisan-craft-gifts/map/