Best Way to use thumbnail Images in PHP

Hi there

I am working on a web project which has a lot of images.My requirement is to create thumbnail or to reduce size of Image.

I have two seneraio to create thumbnail.

1)Either I can send full complete Image to clients via REST and client(Web,Mobile) will generate thumbnail in their script.

2)I will send thumbnail generated from Images to REST client(Web,Mobile) so they do not generate any thumbnail from
uploaded file.They will directly receive thumbnail converted image

Which option I should go?

First one is not an option at all. There is still paid or limited traffic for many clients, and the former approach can cost them a lot. Besides, sending large chunks of data may slow down the overall performance of your app.

1 Like

ok @colshrapnel so you are suggesting I should generate thumbnail before sending Images path to REST clients?

It is not me actually. That’s is how everyone is doing it.

If the intention is for users to have thumbnails instead of full images. then it is better to serve thumbnail images rather than serve full images only to resize them client side.

If you don’t want to use an image app to create thumbnails and upload them to your server PHP can create new images easily enough with good results.

1 Like

@Mittineague

Thanks

Ok to resize at server side an Image What Should I use

right now I am using this code

  `$filelocation='old file location';
   $newfilelocation='new file location';
   $size = getimagesize($filelocation);
  $width=$size[0];//might need to be ['1'] im tired .. :)
  $height=$size[1];
  // Plz note im not sure of units pixles? & i could have the width and height   confused
  //just had some knee surgery so im kinda loopy :) 
  $newwidth = 760;
  $newheight = $height*($newwidth/$width); 


  $pic = new Imagick( $filelocation);//specify name
  $pic->resizeImage($newwidth,$newhight,Imagick::FILTER_LANCZOS,1);
  //again might have width and heing confused
  $pic->writeImage($newfilelocation);//output name
  $pic->destroy();`

I am getting error " Class ‘Imagick’ not found"

There are actually three possibilities:

1.create the thumbnails before uploading the images in the first place so as to upload both versions together.
2. Create the thumbnails from the full sized images as soon as the full sized images are uploaded.
3. Create the thumbnalis from the full sized images just prior to sending them to the client for the first time.

1 Like

That’s what this very ancient and poorly written code I wrote did (in a “if thumbnail doesn’t exist” conditional)

$isrc = $p.$pgimgs[$i]; // original image source
$tn = "tn_".$pgimgs[$i]; // thumbnail name
if (!file_exists($p.$tn))
{
	$img=imagecreatefromjpeg($isrc); 
	$ow=imagesx($img); 
	$oh=imagesy($img); 
	$scale=$nw/$ow; 
	$nh=ceil($oh*$scale); 
	$newimg=imagecreatetruecolor($nw,$nh);
	imagecopyresized($newimg,$img,0,0,0,0,$nw,$nh,$ow,$oh);
	imagejpeg($newimg, $p.$tn, 100);
	imagedestroy($newimg);
}
clearstatcache(); // clean up if exists allocation
$tnsize = getimagesize($p.$tn);
echo "<td><a href='" . $refpg . "?pic=" . $i . "&amp;top=1' rel='nofollow'>";
echo "<img src='" . $p.$tn . "' width='" . $tnsize[0] . "' height='" . $tnsize[1] ."' alt='" . $alt . "' />";
echo "</a></td>";

@felgall

I will go for option-2

I have just use your code but I am getting undefined variable error in $p,$pgimgs,$tn

is there any library or extension require for this code?

That’s bad user experience. First one to hit a gallery with no thumbnails created yet will have to wait forever.

Yes, the variable names aren’t the best, hopefully I’ve gotten better since then.

The code used
GD and Image Functions
http://php.net/manual/en/ref.image.php

Yes, agreed.
In my case I was the first user and I didn’t mind the wait.

It’s been 17 years (I did say ancient) and I had a dial-up cnx at the time, but unless my newbie excitement affected my perception of time I don’t remember it taking all that long. (max ~10 thumbnails per page and they were 125x94 created from 400x300)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.