Ok i’ve been at this for a while and need some help.
I have a directory of 3000+ images that i need thumbnails of, and i can’t quite get php to do it 🙁
I have never made thumbnails with php so this is new to me, I found a few scripts that create thumbnails as you upload images to the server, I will do this from here on out.
The image locations are stored as a URL in a mysql database.
I need it to create a thumbnail of the image and of course, update the database with the thumbnail url.
This is the script I’m using to integrate my needs into:
http://net.tutsplus.com/articles/news/how-to-dynamically-create-thumbnails/
I’m using the exact same functions.php & config.php files.
Here’s what I got:
$row[‘image’] is the location of each image ex: http://www.url.com/images/image.jpg
$filename I’m stripping the url so it just serves image.jpg
<?php
require 'config.php';
require 'functions.php';
require 'db.php';
$sql = mysql_query("SELECT * FROM pics");
while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
$filename = preg_replace('['.$url.']', '', $row['image']);
$source = $row['image'];
createThumbnail($filename);
$thumb_loc = $url . "images/thumbs/" . $filename;
$id = $row['id'];
mysql_query("UPDATE pics SET image_thumb = '$thumb_loc' WHERE id = '$id'");
}
?>
I’m serving a proper filename to $filename and image source to $source, so it should be working..
Here is the createThumbnail function in functions.php:
<?
function createThumbnail($filename) {
require 'config.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 = $final_height_of_image;
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($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);
$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 $tn;
}
?>
Here are the warnings:
Warning: imagecreatefromjpeg(images/fullsized/image.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in functions.php on line 7
Warning: imagesx(): supplied argument is not a valid Image resource in functions.php on line 14
Warning: imagesy(): supplied argument is not a valid Image resource in functions.php on line 15
Warning: Division by zero in functions.php on line 18
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in functions.php on line 20
Warning: imagecopyresized(): supplied argument is not a valid Image resource in functions.php on line 22
Warning: imagejpeg(): supplied argument is not a valid Image resource in functions.php on line 30
Can someone help me out here?
The problem appears to be that path you are specifying for the image file to manipulate is not valid from the location of the script.
The file in the example is
images/fullsized/image.jpgand the relevant error isfailed to open stream: No such file or directory in functions.php on line 7You will need to use an absolute path when telling
imagecreatefromjpegwhat file to open e.g.$_SERVER['DOCUMENT_ROOT'] . '/images/fullsized/image.jpg'or/home/yoursite/public_html/images/fullsized/image.jpg.All of the other error messages are just side effects of the image failing to open. Because the path was incorrect, no GD image was created and therefore the functions being called on it will not work.