473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Creating a 128x128 thumbnail

pek
Hello, I have a simple question which I can't figure out:
I want to create a thumbnail of an image, but I want to make sure that
the output is exactly 128x128.. The resized thumbnail has to be
centered.
Can you please help me?

Thank you very much,
Panagiotis
Jul 14 '08 #1
5 1282
pek
Oops, I kinda rushed and posted it without further explaining. Anyway,
to create this 128x128, I meant not the image will be resized at
128x128 (distorting the perspective), but it would be resized with
maximum width or height at 128, and the space that's left (on top/
bottom or left/right) will be filled with transparent background. I
hope I made my self clear.

Sorry for my bad English.
Jul 14 '08 #2
function createthumb($name,$filename,$new_w,$new_h){
$system=explode('.',$name);
if (preg_match('/jpg|jpeg|JPG|JPEG/',$system[1])){
$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png|PNG/',$system[1])){
$src_img=imagecreatefrompng($name);
}

$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thum b_w,$thumb_h,
$old_x,$old_y);


if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
//call the function with this:
//createthumb('test.jpg','tn_apple.jpg',150,150);
Jul 14 '08 #3
..oO(macca)

Some little hints:
>function createthumb($name,$filename,$new_w,$new_h){
$system=explode('.',$name);
What about image names containing a dot? Better use pathinfo():

$ext = pathinfo($name, PATHINFO_EXTENSION);
if (preg_match('/jpg|jpeg|JPG|JPEG/',$system[1])){
Make use of the power of regular expressions:

if (preg_match('/jpe?g/i', $ext)) {
$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png|PNG/',$system[1])){
if (preg_match('/png/i', $ext)) {
$src_img=imagecreatefrompng($name);
}

$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
You can drop the last part. Just add the equalness to one of the
previous operations (<= or >=). Keeps the code a bit shorter.
>if (preg_match("/png/",$system[1]))
With your original code this part will fail if the extension is PNG. At
the beginning of the function you checked for both lower- and uppercase,
here you don't. Better:

if (preg_match('/png/i', $ext)) {

To avoid this preg_match() call, you could also set a variable at the
beginning, which file format to use. Then it would be as simple as

if ($mode == 'png') {

Micha
Jul 14 '08 #4
A few improvements there thanks :-)
Jul 17 '08 #5
..oO(macca)
>A few improvements there thanks :-)
You're welcome.

Micha
Jul 17 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James Dean | last post by:
In my program i create a 32ARGB bitmap. To actually create the bitmap seems very slow. In fairness the file size is very big. I support multiple bitmaps as well. I have to convert these bitmaps to...
1
by: James Dean | last post by:
I have thought about creating a thumbnail from my full image but its too slow. What is the best algorithm to use for creating a thumbnail directly from data by yourself. What algorithm do microsoft...
2
by: asad | last post by:
Hello friends, how ru all, i have some problem about saving created thumbnail, following is the code i use for creating thumbnail but thumbnail was not saved it is on memory which method is used to...
1
by: David Lozzi | last post by:
Hello, I'm wondering whats the best method to use for displaying several photos' thumbnails. One method I know is to dynamically resize the photo at the time the page is loaded. What does this...
1
by: savvy | last post by:
I want to create a Thumbnail Image from a given large Image and bind it to the Datalist with the datasource being Database. I followed this link given below Creating Thumbnail Images on the fly...
2
by: Duane Phillips | last post by:
On image upload, I would like to save the image and also auto-create a right-size image for faster browsing, and also a thumbnail for indexes, so that slower connections do not have to wait for the...
3
by: tshad | last post by:
oThumbnail.Save is giving me an error: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. theFileName = New FileInfo(strFileName) fileOut =...
9
by: pek | last post by:
Here is what I want: I have an image, let's say a JPEG 800x600... I want to create a transparent 128x128 PNG image and then resize the 800x600 to fit in the center of my transparent 128x128 PNG.....
2
by: djpaul | last post by:
Hello, I have this program and when i want to load pictures it crashes at the form.showdialog()....??? Here it goes: Private Sub CmbPath_SelectedIndexChanged(ByVal sender As Object, ByVal e As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.