Unable to create thumbnail

hi SamA74

i will go with the proportional height at present while keeping width fixed.

but can you tell me how to create two thumbnails instead of one.

here is my working script which creates one thumbnail of 200 width only.

how can i create another thumbnail also with it of 500 width also

so that on every single image i get 2 thumbnails generated and uploaded of 200 and 500

<?php chmod('images/', 0777);
require_once("functions.php"); 
require_once("path.php");  
 
if(isset($_FILES['prod_image'])) 
{           
    if(preg_match('/[.](jpg)|(pjpeg)|(gif)|(png)$/', $_FILES['prod_image']['name']))
    { 
        $path=$_FILES['prod_image']['name'];
        $filename = $_FILES['prod_image']['name'];
        $source = $_FILES['prod_image']['tmp_name'];   
        $target = $path_to_image_directory . $filename;
        move_uploaded_file($source, $target);   
        createThumbnail($filename);
    }
}  
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input name="prod_image" type="file" id="prod_image" size="60" /> <br /><br />   
<input type="submit" value="upload" name="submit" />
</form>
</body>
</html>

here is functions.php

<?
function createThumbnail($filename) {   
       
       require 'path.php';   
       
    if(preg_match('/[.](jpg)$/', $filename)) {   
        $im = imagecreatefromjpeg($path_to_image_directory . $filename);   
    } else if (preg_match('/[.](gif)$/', $filename)) {   
        $im = imagecreatefromgif($path_to_image_directory . $filename);   
    } else if (preg_match('/[.](png)$/', $filename)) {   
        $im = imagecreatefrompng($path_to_image_directory . $filename);   
    }   
       
    $ox = imagesx($im);   
    $oy = imagesy($im);   
       
    $nx = $final_width_of_image;   
    $ny = floor($oy * ($final_width_of_image / $ox));   
       
    $nm = imagecreatetruecolor($nx, $ny);   
       
    imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);     
       
    if(!file_exists($path_to_thumbs_directory)) {   
      if(!mkdir($path_to_thumbs_directory)) {   
           die("There was a problem. Please try again!");   
      }    
       }   
  
    imagejpeg($nm, $path_to_thumbs_directory . $filename,70);   

    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';   
$tn .= '<br />Congratulations. Your file has been successfully uploaded, and a thumbnail has been created.'; 
echo "<p align=center>". $tn  . "</p>";
}  
?>

path.php

<?
$final_width_of_image = 200;   
$path_to_image_directory = 'images/';   
$path_to_thumbs_directory = 'images/thumbs/';  
?>

ok for creating two thumbnails i tried creating one more second function inside functions.php file

then i created new path file also ā€œpath2.phpā€ and change dimensions in it and included it inside second function

now there are 2 functions

function createThumbnail($filename) 
function createThumbnail2($filename) 

But i am still able to generate only one thumbnail.

how to create second thumbnail together ??

<?
function createThumbnail($filename) {   
       
       require 'path.php';   
     
       
    if(preg_match('/[.](jpg)$/', $filename)) {   
        $im = imagecreatefromjpeg($path_to_image_directory . $filename);   
    } else if (preg_match('/[.](gif)$/', $filename)) {   
        $im = imagecreatefromgif($path_to_image_directory . $filename);   
    } else if (preg_match('/[.](png)$/', $filename)) {   
        $im = imagecreatefrompng($path_to_image_directory . $filename);   
    }   
       
    $ox = imagesx($im);   
    $oy = imagesy($im);   
       
    $nx = $final_width_of_image;   
    $ny = floor($oy * ($final_width_of_image / $ox));   
       
    $nm = imagecreatetruecolor($nx, $ny);   
       
    imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);     
       
    if(!file_exists($path_to_thumbs_directory)) {   
      if(!mkdir($path_to_thumbs_directory)) {   
           die("There was a problem. Please try again!");   
      }    
       }   
  
    imagejpeg($nm, $path_to_thumbs_directory . $filename,70);   

    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';   
$tn .= '<br />Congratulations. Your file has been successfully uploaded, and a thumbnail has been created.'; 
echo "<p align=center>". $tn  . "</p>";
}  

function createThumbnail2($filename) {   
       
       require 'path2.php';   
     
       
    if(preg_match('/[.](jpg)$/', $filename)) {   
        $im = imagecreatefromjpeg($path_to_image_directory . $filename);   
    } else if (preg_match('/[.](gif)$/', $filename)) {   
        $im = imagecreatefromgif($path_to_image_directory . $filename);   
    } else if (preg_match('/[.](png)$/', $filename)) {   
        $im = imagecreatefrompng($path_to_image_directory . $filename);   
    }   
       
    $ox = imagesx($im);   
    $oy = imagesy($im);   
       
    $nx = $final_width_of_image;   
    $ny = floor($oy * ($final_width_of_image / $ox));   
       
    $nm = imagecreatetruecolor($nx, $ny);   
       
    imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);     
       
    if(!file_exists($path_to_thumbs_directory)) {   
      if(!mkdir($path_to_thumbs_directory)) {   
           die("There was a problem. Please try again!");   
      }    
       }   
  
    imagejpeg($nm, $path_to_thumbs_directory . $filename,70);   

    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';   
$tn .= '<br />Congratulations. Your file has been successfully uploaded, and a thumbnail has been created.'; 
echo "<p align=center>". $tn  . "</p>";
}  
?>

this is path.php

<?
$final_width_of_image = 200;   
$path_to_image_directory = 'images/';   
$path_to_thumbs_directory = 'images/thumbs/';  
?>

this is path2.php

<?
$final_width_of_image = 500;   
$path_to_image_directory = 'images/';   
$path_to_thumbs_directory = 'images/thumbs/';  
?>

It would be smarter to create just one function where the width is a variable. Eg

function createThumbnail($filename, $width) {}

Or the predefined widths could exist in the function, possibly in an array followed by a foreach. You then need to differentiate either the path or filename.

ok i tried a small code from php.net with variable width and height

but it still echoes one image output only

can you tell me how to make it work

may be i am not passing values in the correct way ???

<?php
function thumb($width,$height){
// The file
$filename = 'http://localhost/thumb_script/images/s1.jpg';

// Set a maximum height and width
//$width = 200;
//$height = 200;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
}
thumb(200,200);
thumb(500,500);
?>

hi

its getting complicated for me to understand code and modify above code it was not written by me

so i think i need to start it from zero to understand it

so i created a simple upload script which works fine and uploads image.

i m not involving validation at present.

<?php
if($_FILES['photo']['name'])
{
    echo $file_name = $_FILES['photo']['name'];
    echo "<br>";
    echo $file_tmp =$_FILES['photo']['tmp_name'];
    
    move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$_FILES['photo']['name']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
    Your Photo: <input type="file" name="photo" size="25" />
    <input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

now i have this code that resizes the image

// The file
$filename = 'images/s1.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);

so i tried to combine them as they both work fine independently

<?php
if($_FILES['photo']['name'])
{
    echo $file_name = $_FILES['photo']['name'];
    echo "<br>";
    echo $file_tmp =$_FILES['photo']['tmp_name'];
    
    //resize function starts
    $filename = 'images/'.$file_name;

    // Set a maximum height and width
    $width = 200;
    $height = 200;

    // Get new dimensions
    list($width_orig, $height_orig) = getimagesize($filename);

    $ratio_orig = $width_orig/$height_orig;

    if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
    } else {
    $height = $width/$ratio_orig;
    }

    // Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

    // Output
    imagejpeg($image_p, null, 100);
    // resize function ends
    
    move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$_FILES['photo']['name']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
    Your Photo: <input type="file" name="photo" size="25" />
    <input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

so i have two questions related to above combined code

  1. The resample or resize function code will come before or after ā€œmove_uploaded_fileā€ function

  2. what will i write in the ā€œmove_uploaded_fileā€ function so that resized thumbnail gets uploaded

vineet

Your createThumbnail function needs at least two if not three parameters

  1. the filename to be resized
  2. the width of the image to create
  3. optionally the height of the image to create.

Then you just call the function twice to create two images of different sizes just by changing the second parameter.

@vinpkl

I thought this thumbnail creation may also be useful to me so created the following:

Online Demo

All source code included.

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