Jump to content

How to create a thumbnail on the fly


adam291086

Recommended Posts

i am using the code by craygo, thanks very much. Although i am getting no images, just a box with a red cross. Heres the code

<?php


$folder = glob("images/*.jpg"); 

foreach($folder AS $file)
{




//large image
$large = $file;
//thumbnail

?>
	<td>
	<img src="imageresize.php?maxsize=xxx&source=<?php echo $file ?>" border=0 />
	</td>
	<td>

<?php
    }
?>

 

when i look at the html source code of the images i get

<td>
	<img src="imageresize.php?maxsize=xxx&source=images/Picture 043.jpg" border=0 />
	</td>
	<td>

 

 

 

Link to comment
Share on other sites

I copied your exact code, I happen to have an images folder also :) , and it works fine.

 

thumbs.php

<?php
$folder = glob("images/*.jpg");

foreach($folder AS $file)
{
//large image
$large = $file;
//thumbnail
?>
	<td>
	<img src="imageresize.php?maxsize=200&source=<?php echo $file; ?>" border=0 />
	</td>
	<td>

<?php
    }
?>

 

and imageresize.php

<?php
/**********************************
*  Will resize an image to a      *
*  max width or height and keep   *
*  aspect ratio Name this file    *
*  anything you like.             *
*  I will use imageresize.php     *
**********************************/

header('Content-type: image/jpeg');
function resampleimage($maxsize, $sourcefile, $imgcomp=0){
// SET THE IMAGE COMPRESSION
$g_imgcomp=100-$imgcomp;
  // CHECK TO SEE IF THE IMAGE EXISTS FIRST
  if(file_exists($sourcefile)){
  // FIRST WE GET THE CURRENT IMAGE SIZE
  $g_is=getimagesize($sourcefile);
    /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/
    // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE
    if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
    // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE
    $new_width=$g_is[0];
    $new_height=$g_is[1];
    } else {
    // GET VALUE TO CALCULATE WIDTH AND HEIGHT
    $w_adjust = ($maxsize / $g_is[0]);
    $h_adjust = ($maxsize / $g_is[1]);
      // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT
      if($w_adjust <= $h_adjust){
      // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER
      $new_width=($g_is[0]*$w_adjust);
      $new_height=($g_is[1]*$w_adjust);
      } else {
      // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER
      $new_width=($g_is[0]*$h_adjust);
      $new_height=($g_is[1]*$h_adjust);
      }
    }
  //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." )
$image_type = strrchr($sourcefile, ".");

//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($image_type) {
	case '.jpg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.jpeg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.png':
		$img_src = imagecreatefrompng($sourcefile);
		break;
	case '.gif':
		$img_src = imagecreatefromgif($sourcefile);
		break;
	default:
		echo("Error Invalid Image Type");
		die;
		break;
}
  // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT
  $img_dst=imagecreatetruecolor($new_width,$new_height);
  // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT
  imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
  // OUTPUT THE IMAGE AS A JPEG.
  // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE.
  imagejpeg($img_dst);
  // DESTROY THE NEW IMAGE
  imagedestroy($img_dst);
  return true;
  } else {
  return false;
  }
}
// NOW CALL THE IMAGE FROM ANY OTHER PAGE WITH <img src="imageresize.php?maxsize=xxx&source=path/to/image/file" border=0 /> xxx=a value for the max size
resampleimage($_GET['maxsize'], $_GET['source']);
?>

 

They cannot be in the same file. If it is not working I am not sure why. I copied and pasted your code verbatim and it works fine.

 

Ray

Link to comment
Share on other sites

still having problems here is the current situation.

 

I have a folder called gallery and in here there are the imageresizer script and a template.php that calls on the imagersizer script. The images are coming from a folder called imager which is in the gallery folder.

 

Is this correct

Link to comment
Share on other sites

It could be that the script can't handle the space in your image title (perhaps "Picture_043.jpg" would work). Maybe you need to echo it out in ASCII?

 

<img src="imageresize.php?maxsize=xxx&source=images/Picture 043.jpg" border=0 />

Link to comment
Share on other sites

ok,

 

 

i have a folder in the root directory of my server called gallery.

 

In here i have;

  • image.php which is the script you gave me
  • template.php which calls on your script to generate the images
  • a folder call images which contains all my images

 

these are all the scripts

 

image.php

<?php
error_reporting(E_ALL);
/**********************************
*  Will resize an image to a      *
*  max width or height and keep   *
*  aspect ratio Name this file    *
*  anything you like.             *
*  I will use imageresize.php     *
**********************************/

header('Content-type: image/jpeg');
function resampleimage($maxsize, $sourcefile, $imgcomp=0){
// SET THE IMAGE COMPRESSION
$g_imgcomp=100-$imgcomp;
  // CHECK TO SEE IF THE IMAGE EXISTS FIRST
  if(file_exists($sourcefile)){
  // FIRST WE GET THE CURRENT IMAGE SIZE
  $g_is=getimagesize($sourcefile);
    /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/
    // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE
    if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
    // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE
    $new_width=$g_is[0];
    $new_height=$g_is[1];
    } else {
    // GET VALUE TO CALCULATE WIDTH AND HEIGHT
    $w_adjust = ($maxsize / $g_is[0]);
    $h_adjust = ($maxsize / $g_is[1]);
      // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT
      if($w_adjust <= $h_adjust){
      // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER
      $new_width=($g_is[0]*$w_adjust);
      $new_height=($g_is[1]*$w_adjust);
      } else {
      // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER
      $new_width=($g_is[0]*$h_adjust);
      $new_height=($g_is[1]*$h_adjust);
      }
    }
  //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." )
$image_type = strrchr($sourcefile, ".");

//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($image_type) {
	case '.jpg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.jpeg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.png':
		$img_src = imagecreatefrompng($sourcefile);
		break;
	case '.gif':
		$img_src = imagecreatefromgif($sourcefile);
		break;
	default:
		echo("Error Invalid Image Type");
		die;
		break;
}
  // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT
  $img_dst=imagecreatetruecolor($new_width,$new_height);
  // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT
  imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
  // OUTPUT THE IMAGE AS A JPEG.
  // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE.
  imagejpeg($img_dst);
  // DESTROY THE NEW IMAGE
  imagedestroy($img_dst);
  return true;
  } else {
  return false;
  }
}
// NOW CALL THE IMAGE FROM ANY OTHER PAGE WITH <img src="imageresize.php?maxsize=xxx&source=path/to/image/file" border=0 /> xxx=a value for the max size
resampleimage($_GET['maxsize'], $_GET['source']);
?>

 

 

template.php

<?php
error_reporting(E_ALL);
$folder = glob("images/*.jpg");

foreach($folder AS $file)
{
//large image
$large = $file;
//thumbnail
?>
	<td>
	<img src="image.php?maxsize=200&source=<?php echo $file ?>" border=0 />
	</td>
	<td>

<?php
    }
?>

 

 

Link to comment
Share on other sites

is the web server yours?? Do you have access to the php.ini file??

 

To find out if it is on just make a temp page and inside put in

<?php
phpinfo();
?>

 

This will output php setting about a third of the way down the page there should be a section called gd. if it's enabled then something unknown is going on, but if it is not there or sayd disabled then you have to activate it. And you can only do that in the php.ini file.

 

Ray

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.