resiseing images with php

Status
Not open for further replies.

SyncViews

New Member
Messages
37
Reaction score
0
Points
0
How do I resie an image in php?

I want it to copy
'./images/' . $file . '.jpg'
to...
'./images/thumbnails/' . $file . '.jpg'

which I think I can do with copy();

But then how do I resize the image I just copied?
 

adrenlinerush

New Member
Messages
379
Reaction score
1
Points
0
you can force the image size displayed without doing that...

here is the html... your php just has to generate it....

<img style="border: 0px solid ; width: 300px; height: 80px;" alt="alternate text" src="images/name.jpg">
 

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
you can force the image size displayed without doing that...

here is the html... your php just has to generate it....

<img style="border: 0px solid ; width: 300px; height: 80px;" alt="alternate text" src="images/name.jpg">


Doesn't that destroy the purpose of a thumbnail? They are meant to be small images that load fast.

PHP:
function createthumb($name,$filename,$new_w,$new_h){
    $system=explode('.',$name);
    if (preg_match('/jpg|jpeg/',$system[1])){
        $src_img=imagecreatefromjpeg($name);
    }
    if (preg_match('/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,$thumb_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); 
}


Let's say you have an original picture called orange.jpg in the folder pics and you want to generate the thumbnail in the folder thumbs as tn_apple.jpg with the dimensions of 100x100 pixels

You can do this by calling this function with the following parameters:

PHP:
createthumb('pics/orange.jpg','thumbs/tn_orange.jpg',100,100);

This function attempts to maintain the aspect ratio.
 

SyncViews

New Member
Messages
37
Reaction score
0
Points
0
yeah. the reason I want them Is cause otherwise some pages are like 5mb lol.

I did try this but it gave me a black box as the resulting jpg :(
PHP:
<?php
	$file_full  = './images/' . $file;
	$file_thumb = './images/thumbnails/' . $file;
	
	$image_full  = imagecreatefromjpeg ($file_full);
	$image_thumb = imagecreatetruecolor(400, 300);
	
	$image_image_x = imagesx($image_full);
	$image_image_y = imagesy($image_full);
	
	imagecopyresampled ($image_thumb, $image_full, 0, 0, 0, 0, 400, 300, $file_image_x, $file_image_y);
	//imagedestroy ($image_full); //get some memory back
	imagejpeg($image_thumb, $file_thumb, 80);
?>

Wioll try the above method now :)

EDIT: why can't bb cpode by standedised...
PHP:
 woprked over in the other forum/
 
Last edited:

adrenlinerush

New Member
Messages
379
Reaction score
1
Points
0
yeah i agree the purpose is so the thumbs load fast and it probably matters with the jpg format... but probably not so much in a more compressed format such as png or gif...
 

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
yeah i agree the purpose is so the thumbs load fast and it probably matters with the jpg format... but probably not so much in a more compressed format such as png or gif...

Lets see... a 1024x768 png would not load much slower than say a 100x100 one? I'd say it does :)
 

adrenlinerush

New Member
Messages
379
Reaction score
1
Points
0
when the file size is already only 50k its not going to matter on a high speed connection
 

SyncViews

New Member
Messages
37
Reaction score
0
Points
0
No except when free hosting isn't that massively fast and theres like 50 pictures in a gallery. Even if the thumbnails are 20% smaller file sizes it helps...

edit: and I still don't know why my simplefied code gives me a black box :(
 
Last edited:
Status
Not open for further replies.
Top