Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3910 > unrolled thread

Thumbnails with PHP

Started byNiels Lange <semanticsamsterdam@gmail.com>
First post2011-11-24 01:51 -0800
Last post2011-11-24 11:10 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Thumbnails with PHP Niels Lange <semanticsamsterdam@gmail.com> - 2011-11-24 01:51 -0800
    Re: Thumbnails with PHP "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-11-24 11:06 +0100
    Re: Thumbnails with PHP The Natural Philosopher <tnp@invalid.invalid> - 2011-11-24 11:10 +0000

#3910 — Thumbnails with PHP

FromNiels Lange <semanticsamsterdam@gmail.com>
Date2011-11-24 01:51 -0800
SubjectThumbnails with PHP
Message-ID<7c34a42a-ec30-4c0f-95c3-634a0d2677b6@q9g2000yqe.googlegroups.com>
Hi there,

at present I'm developing a website for a photographer. Currently I'm
using PHPThumb (http://phpthumb.gxdlabs.com/) to create thumbnails
resp. to trim the uploaded photos. The problem is that I have to
define a fixed upload format. Therefore it's only possible to create
horizontal images but no vertical images.

In detail my clients expectations are as follows:

- If he uploads a horizontal image it'll get cropped to a certain
size, e.g. 800p x 600 pixel.
- If he uploads a vertical image the image will cropped with a height
of 600 pixel, centered in a 800 pixel width frame. Than that frame
should be filled black.

I'm not really sure how to realize that. Any suggestions? Thank you in
advanced!

Regards,
Niels

[toc] | [next] | [standalone]


#3911

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2011-11-24 11:06 +0100
Message-ID<jal4v2$ddl$1@dont-email.me>
In reply to#3910
El 24/11/2011 10:51, Niels Lange escribió/wrote:
> at present I'm developing a website for a photographer. Currently I'm
> using PHPThumb (http://phpthumb.gxdlabs.com/) to create thumbnails
> resp. to trim the uploaded photos. The problem is that I have to
> define a fixed upload format. Therefore it's only possible to create
> horizontal images but no vertical images.

I've never used that library but here's the first example I could find 
in their manual:

<?php

require_once 'path/to/ThumbLib.inc.php';

try
{
      $thumb = PhpThumbFactory::create('/path/to/image.jpg');
}
catch (Exception $e)
{
      // handle error here however you'd like
}

$thumb->resize(100, 100);
$thumb->show();

?>

https://github.com/masterexploder/PHPThumb/wiki/Basic-Usage

So it's pretty obvious that the resize() method accepts a bounding box 
size when you call it. You don't have to define a fixed size as you 
claim. The same seems to happen with the other methods.

> In detail my clients expectations are as follows:
>
> - If he uploads a horizontal image it'll get cropped to a certain
> size, e.g. 800p x 600 pixel.
> - If he uploads a vertical image the image will cropped with a height
> of 600 pixel, centered in a 800 pixel width frame. Than that frame
> should be filled black.

Do you mean that horizontal images are cropped and vertical images are 
resized? From the name (there's no further documentation as far as I 
see) it seems that the getCurrentDimensions() method returns the image 
size. An image is horizontal if width/height is greater than 1. Use that 
to write a conditional that calls either crop() or resize().

-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

[toc] | [prev] | [next] | [standalone]


#3913

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-11-24 11:10 +0000
Message-ID<jal8ml$3p6$1@news.albasani.net>
In reply to#3910
Niels Lange wrote:
> Hi there,
> 
> at present I'm developing a website for a photographer. Currently I'm
> using PHPThumb (http://phpthumb.gxdlabs.com/) to create thumbnails
> resp. to trim the uploaded photos. The problem is that I have to
> define a fixed upload format. Therefore it's only possible to create
> horizontal images but no vertical images.
> 
> In detail my clients expectations are as follows:
> 
> - If he uploads a horizontal image it'll get cropped to a certain
> size, e.g. 800p x 600 pixel.
> - If he uploads a vertical image the image will cropped with a height
> of 600 pixel, centered in a 800 pixel width frame. Than that frame
> should be filled black.
> 
> I'm not really sure how to realize that. Any suggestions? Thank you in
> advanced!
> 

some code may help you  - this uses the GD library.
You will have too rejig the logic to get the size YOU want.
Also be aware that for large images, this is pretty CPU intensive.
Likewise, there are some potential issues with te GD library not being 
re-entrant.. simultaneous production of images MAY have caused some 
problems on a busy site.
If you can bear it, its probably better to use code like this ONCE when 
UPLOADING the image to generate a thumbnail.


<?php
include('shoplib.php'); // contains database access stuff..
open_database(); // ready to check
$id=$_GET['id']; // call as thumbnail.php?id=2395 or whatever
/*************************************
* extract pictre sa BLOB from database
**************************************/
$query="select picture from product where id='".$id."'";
$result=mysql_query($query);
if(($result>0) && (($rows=mysql_numrows($result)) == 1)) //got some data
	{
	$content=mysql_result($result,0,'picture');
	}
else die();
if ($name="") die();

// now to shrink the picture..
$im=imagecreatefromstring($content);
// GD lib handles nearly all image types
// get sizes
$width=imagesx($im);
$height=imagesy($im);
// our thumbnails are 100px wide..dont care about the height so scale as 
width
$newheight=round(($height*100)/$width);
$newwidth=100;
// we DO care about the height now, so if the height is more than 70px, 
scale that
if($newheight>50)
	{
	$newwidth=round(100*50/$newheight);
	$newheight=50;
	}
$thumbnail=imagecreatetruecolor($newwidth,$newheight);
// make empty new wotsit.
imagecopyresampled($thumbnail, 
$im,0,0,0,0,$newwidth,$newheight,$width,$height);
header("Content-Type: image/jpeg");
imagejpeg( $thumbnail,null,75); ?>
?> // and remmove ANY trailing whitespace after this..

> Regards,
> Niels

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web