I run this script via cron daily to test for images ( I have a system that updates product quantities and status from our brick-and-mortar POS system ). All of the YOUR_* info has to be replaced with your local info... I haven't been running this long, but it seems to do the job for me.
This script looks at what active products have defined and tests if it is there. If it is not there, it ouputs "/YOUR_IMAGE_DIRECTORY/YOURIMAGE missing"
If it finds the file, it tests for a specific issue I have with JPG's - it tries to "fix" the jpg and outputs what is what.
PHP Code:
session_start();
$tim = localtime(time(),true);
echo "product image check start..." .
$tim['tm_hour'].":".$tim['tm_min'].":".$tim['tm_sec'] . "\n";
$db = mysql_connect("localhost", "YOUR_DB_LOGINID", "YOUR_DB_PASSWORD") or die("Could not connect.");
if(!$db) die("no db");
if(!mysql_select_db("YOUR_DB_NAME",$db)) die("No database selected.");
// iterate through active products to find missing images.
// z_ is my zen table prefix - it is required here.
// only care about active products
$result = mysql_query( " select products_image from z_products where products_status = 1 order by products_model") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$image='YOUR_IMAGE_DIRECTORY' . $row['products_image'];
if ( !file_exists( $image ) )
{
echo $image . " missing\n";
}
else
{
// test jpg's
// I get some images from vendors that use
// 4 color channel jpg's (CMYK)
// browsers can not display them correctly - or ih2
$getimagesize = getimagesize($image);
// test for bad jpg
if ( isset($getimagesize['channels']) &&
$getimagesize['channels'] == 4 &&
$getimagesize[2] == IMAGETYPE_JPEG )
{
// try to convert the jpg to 3 color channel (RGB)
$im = @imagecreatefromjpeg($image);
if ($im) {
rename( $image, $image . '.old' );
imagejpeg($im, $image, 100);
imagedestroy($im);
echo $image . " converted...\n";
}
else
{
echo $image . " bad jpg - cannot convert \n";
}
}
}
}
$tim = localtime(time(),true);
echo "\nproduct image check finished..." .
$tim['tm_hour'].":".$tim['tm_min'].":".$tim['tm_sec'] . "\n";
Bookmarks