Unable to create thumbnail

hi
everytime i try to upload image to create thumbnail i get error


Warning:  imagecreatefromjpeg(gadmagento-error.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in E:\xampp\htdocs\thumb_script\thumb_script.php on line 5

Warning:  imagesx() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script.php on line 12

Warning:  imagesy() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script.php on line 13

Warning:  Division by zero in E:\xampp\htdocs\thumb_script\thumb_script.php on line 16

Warning:  imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in E:\xampp\htdocs\thumb_script\thumb_script.php on line 18

Warning:  imagecopyresampled() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script.php on line 20
Warning:  mkdir() [function.mkdir]: Invalid argument in E:\xampp\htdocs\thumb_script\thumb_script.php on line 23
There was a problem. Please try again!

i have images folder and inside it i have thumbnail folder

only the original big images is getting upload in images folder.

thumbnail is not getting created

i have chmod images directory also

here is php script

<?php chmod('images/', 0777);
function createThumbnail($filename) {   
    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>";
}  

$final_width_of_image = 200;   
$path_to_image_directory = 'images/';   
$path_to_thumbs_directory = 'images/thumbs/'; 


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>
<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" 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>

The variables $path_to_image_directory and $path_to_thumbs_directory are not defined in the function.

hi SamA74

i moved path inside the function but still same error

<?php chmod('images/', 0777);
function createThumbnail($filename) { 

$final_width_of_image = 200;   
$path_to_image_directory = 'images/';   
$path_to_thumbs_directory = 'images/thumbs/'; 

  
    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>";
}  


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>
<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" 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 the error

Warning:  imagecreatefromgif(images/16rows.gif) [function.imagecreatefromgif]: failed to open stream: No such file or directory in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 12



Warning:  imagesx() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 16



Warning:  imagesy() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 17



Warning:  Division by zero in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 19



Warning:  imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 20



Warning:  imagecopyresampled() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 21



Warning:  imagejpeg() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 27

There is a little progress, first we saw:-

imagecreatefromjpeg(gadmagento-error.jpg)

Now we have:-

imagecreatefromgif(images/16rows.gif)

…some path in the function, but not the correct path.

Maybe:-

$path_to_image_directory = $_SERVER["DOCUMENT_ROOT"].'/images/'; 

…or whatever is correct.

hi SamA74

i tried it but still getting same error

$path_to_image_directory = $_SERVER["DOCUMENT_ROOT"].'/images/';
$path_to_thumbs_directory = $_SERVER["DOCUMENT_ROOT"].'/images/thumbs/'; 

path still incorrect
E:/xampp/htdocs/images/16rows.gif

here is the error

Warning:  imagecreatefromgif(E:/xampp/htdocs/images/16rows.gif) [function.imagecreatefromgif]: failed to open stream: No such file or directory in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 13



Warning:  imagesx() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 17



Warning:  imagesy() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 18



Warning:  Division by zero in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 20



Warning:  imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 21



Warning:  imagecopyresampled() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 22



Warning:  mkdir() [function.mkdir]: No such file or directory in E:\xampp\htdocs\thumb_script\thumb_script2.php on line 24

There was a problem. Please try again!

You need to work out what the correct path is and define it in the function.

Do a var_dump() on both $path_to_image_directory and $path_to_thumbs_directory to check that both are what you expect them to be

i tried

$path_to_image_directory = dirname(__FILE__).'/images/';

it show path as

E:\xampp\htdocs\thumb_script/images/16rows.gif

there are some frontslashes and some backslashes

may be problem with xampp i dont know

vineet

Try changing:

$path_to_image_directory = dirname(__FILE__).'/images/';

to

$path_to_image_directory = dirname(__FILE__).'\images\';

hi SpacePhoenix

when i change slashes then whole php code color changes in editor dreamweaver

that means there is something wrong ??

vineet

ok i created an include file and put the both paths in that included file and now path is coming up correct.

A new question i want to ask is how can i make a thumbnail of fixed height and width with my script.

and present it only create thumbnail according to max width defined.

means it will keep the max width 200 but shrinks the height.

i dont want to shrink the height.

at present i m uploading image of fixed size created in photoshop.

but now i want to avoid photoshop and create fixed size thumbnail from any big size image being uploaded.

i dont want to shrink the height.

vineet

Get the width and height of the original image. Divide them to get the aspect ratio. The new height will be 200 (new width) divided by the aspect.

$aspect = $width / $height;
$newWidth = 200;
$newHeight = round($newWidth / $aspect);

http://php.net/manual/en/function.imagescale.php

hi SamA74

i tried it with a image of width 850 and height 995

it outputs width 200 but height 234

how can i get equal and maximum 200x200 width height

here is the image

<style type="text/css">
.thumb{width:200px;
height:200px;
border:1px solid #FF0000;
}
</style>
<?php
    $imagePath = "http://localhost/thumb_script/images/s2.gif";
    $imageSize = getimagesize($imagePath);

    $imageWidth = $imageSize[0];
    $imageHeight = $imageSize[1];

    $aspect = $imageWidth / $imageHeight;
    $newWidth = 200;
    $newHeight = round($newWidth / $aspect);

    echo "<div class='thumb'><img src='".$imagePath."' width='".$newWidth."' height='".$newHeight."' /></div>";
?>

output

div class='thumb'><img src='http://localhost/thumb_script/images/s2.gif' width='200' height='234' /></div>

Not sure what you mean.
My code is to make the width be 200px but keeping the original aspect ratio.
Do you want which ever dimension is largest to be 200?
So portrait has height 200 and landscape has width 200?

hi SamA74

Yes i want
any image i upload of any dimension
should generate thumbnail of 200x200

vineet

Don’t know if this will be of any help to you:

imagepng(imagescale(imagecreatefromstring(file_get_contents($_FILES['upload']['tmp_name'])),250,250), $avatar_name);

$avatar_name is the location where the picture produced is placed (in this example it’s going to be used as an avatar). The 250 and the other 250 one is width, one is height, can’t remember off-hand which way round

Because 200x200 is a square, this will work as long as the uploaded images are also square.

If the uploaded images are not square the generated thumbnails will either be more or less distorted or be cropped.

Typically a more important consideration is maintaining the aspect ratio, not forcing a shape of a certain size. (avatars being an exception)

For example, To display side-by-side horizontal it might be important that they all have the same height. To display above-and-below vertical it might be important that they all have the same width.

Also keep in mind that if the uploaded image is a lot larger than the generated thumbnail there may be some loss of detail. Conversely, if much smaller there may be pixelation.

1 Like

ok another question i have

how can i create multiple thumbnails of single image

i mean

one thumbnail of 200x200
another thumbnail of 600x600

from single input field and single image

vineet

You simply run the thumbnail script twice with the same input but using different dimensions for the output.

That will require cropping of non-square images.
http://php.net/manual/en/function.imagecrop.php
or
http://php.net/manual/en/function.imagecopyresampled.php

Your code in post #13 does not resize to a new file, it just displays the image at a reduced size.
If that is all you want to do, that could be done with new css properties.

object-fit: cover;

https://css-tricks.com/almanac/properties/o/object-fit/