用户上传图像时调整图像大小

时间:2013-11-13 19:18:52

标签: php image upload

我一直在靠墙试图调整图像大小没有运气。我曾尝试使用resize-class.php,php_image-magician.php以及其他一些脚本但每次都失败。我还在学习php,所以也许有人可以解释我的问题。 继承我正在使用的代码

     // Loop $_FILES to test all files
                                foreach      ($_FILES['files']['name'] as $f => $name) {   
                                    $total++;   // Total count for images attempted to be uploaded
                                    if ($_FILES['files']['error'][$f] == 4) {
                                        $images .= "'', "; //Empty image if error found
                                        $descs .= "'', "; //Empty description if error      
                                        continue; // Skip file if any error found
                                    }          
                                    if ($_FILES['files']['error'][$f] == 0) {              
                                        if ($_FILES['files']['size'][$f] > $max_file_size) {
                                            $images .= "'', "; // Empty image on image error
                                            $descs .= "'', "; // Empty description on image error
                                            $errorCount++; //Erro counter
                                            $errorer .= " Image Too Large "; // Error feedback for user
                                            continue; // Skip large files
                                        }
                                        elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                                            $images .= "'', "; // Empty image if error found
                                            $descs .= "'', "; // Empty description if error found with image
                                            $errorCount++; //Erro counter
                                            $errorer .= " File must be jpg, png, jpeg, or gif "; //Error feedback for user
                                            continue; // Skip invalid file formats
                                        }
                                        else{ // No error found! Move uploaded files                
                                            $addName = str_replace(':', '', date('Y:m:d:h:i:s')); // Add to the name to make the image unique

                                            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$addName.$name)) {
                                                $newName = $addName.$name;  
                                                $count++; // Number of successfully uploaded file
                                                $catch = "desc".$count; // Description of image
                                                $info = cleanInput(mysql_real_escape_string($_POST[$catch])); // clean input
                                                $descs .= "'$info', "; // Format it for database insert
                                                $images .= "'$newName', "; // Format image path for database

                                            }
                                        }
                                    }

                                } // End Foreach Loop

我在只需要一次上传的文件上使用http://www.w3bees.com/2013/03/resize-image-while-upload-using-php.html resize脚本,但每当我尝试将其添加到我的代码中时,我都会遇到大量错误。我做错了什么?

1 个答案:

答案 0 :(得分:0)

以下代码可帮助您调整图片大小:

$image = new Imagick( $filename );
$imageprops = $image->getImageGeometry();
if ($imageprops['width'] <= 200 && $imageprops['height'] <= 200) {
    // don't upscale
} else {
    $image->resizeImage(200,200, imagick::FILTER_LANCZOS, 0.9, true);
}