I have a php script I'm using to convert an uploaded image into half it's quality so I can save server space, but it isn't working I'm new to PHP so hopefully someone can help me where I am going wrong.
if(isset($_FILES['t1']['name'])){
$file = rand(0, 10000000).$_FILES['t1']['name'];
if (move_uploaded_file($_FILES['t1']['tmp_name'], $file)) {
if($fp = fopen($file,"rb", 0))
{
$picture = fread($fp,filesize($file));
fclose($fp);
$img = imagecreatefrompng($file);
imagepng($img, $file, 6); //6 quality setting
imagedestroy($img);
$tag1 = '<img src="'.$file.'" alt="" class="default" />';
//unlink($file);
echo "<script>$(document).ready(function() {var write = $('.item:nth-child(1)').html();localStorage.item1Pantry = write;});</script>";
}
}
}
PNG is lossless compression. When you set "quality" to 6, you are really just using a mediocre compression method, likely taking up more space. Use 9 for the highest compression (at the expense of slightly more CPU usage, which is hardly noticeable unless you're doing batch processing).
You might also look into Yahoo!'s image compression , for more lossless optimizations.
Also, you really shouldn't be allowing users to specify file names, even in part. You wouldn't be too pleased if someone uploaded remote_control_script.php
. Never trust what users upload.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.