Hey again,
Wow you seem very lost, but let's get there one step at a time, first of all in PHP, this doesn't work,
*quoting your post*
(2014-08-28, 03:31 PM)marcus123 Wrote: [ -> ]<?php
chdir( 'images1' );
//using backticks to run system command
`mogrify -quality 10 *.jpg`;
?>
You can only use
exec(),
system() or such functions to run command lines, as you could see in the code you've posted in the OP.
Now I've done some research for an "image compression script in PHP", and came across this one :
http://zenverse.net/php-reducing-image-f...-using-gd/
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
//save file
imagejpeg($image, $destination_url, $quality);
//return destination file
return $destination_url;
}
//usage
$compressed = compress_image('source.png', 'destination.jpg', 90);
Now, as you may see, the function
compress_image(), would compress the image 'source.png' at a quality of 90%, and save it as 'destination.jpg'.
Note:
- Third parameter,
90, means you reduce the quality by 10%, so if you want a very very bad image, lower this to 50 or even less.
Now you need to iterate over your avatars directory, and compress each image, so the full code would be something like :
<?php
$avatars_dir = "/var/www/mybb_forum_path/uploads/avatars/"; // the full path to your avatars directory
$quality = 80;
$converted_files = @unserialize(file_get_contents("./converted_files.txt"));
// first time? create the array
$converted_files = is_array($converted_files) ? $converted_files : array();
foreach(glob($avatars_dir . "*.*") as $image) {
if(in_array($image, $converted_files)) {
continue;
}
$ext = pathinfo($image, PATHINFO_EXTENSION);
if(preg_match('/^(jpg|jpeg|gif|png)$/i', $ext)) {
$converted = compress_image($image, $image, $quality);
$converted_files[] = $converted;
echo "Successfully converted : <b>" . $converted . "</b><br>";
}
}
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
//save file
imagejpeg($image, $destination_url, $quality);
//return destination file
return $destination_url;
}
@file_put_contents("./converted_files.txt", serialize($converted_files));
CHANGE :
$avatars_dir = "/var/www/mybb_forum/uploads/avatars/";
to the avatars directory of your forum!
ALSO :
- Next to the php file you create, also make an empty file named
converted_files.txt where this script store the files it converts, so it won't re-convert them on next run
.
WARRANTY :
This is a 2 minute script, I didn't spend time on it at all, there's no warranty this would work as I only tested it on one scenario of use, also changing the path of your avatars will cause the script to re-convert them all, as it is path-based which I know is bad but whatever .
Also, I used "@" to hide any PHP errors/warnings which I personally hate using, but ehm let's not comment on that either .
I've tested this on a directory containing a JPEG image weighing 523kb, at a 80% quality it got down to 380kb.