php调整图像建议

时间:2013-06-18 08:53:41

标签: php image resize

我是第一次这样做,所以没有相关信息。我需要调整图像大小。

这是我的代码。

if(!empty($_FILES['img']['name'])){
                        $extension = end(explode(".",$_FILES['img']['name']));

                        $name = $_FILES['img']['name'];
                        $size = $_FILES['img']['size'];

                        if(file_exists("views/admin/uploads/".$name)){
                            $errors[] = "File with this name already exists!";
                        }

                        if($extension != "jpg" && $extension != "png" && $extension != "gif" && $extension != "JPG"){
                            $errors[] = "Unknown file format!";
                        }
                    }

                    if(count($errors)==0){
                        $query = mysql_query("INSERT INTO `avetisyan`.`news` (`id`, `category`, `title`, `img`, `short_content`, `content`, `date`, `lang`) VALUES (NULL, '$category', '$title', '$name', '$short_content', '$long_content', '$date', '$lang')");
                        move_uploaded_file($_FILES['img']['tmp_name'],"views/admin/uploads/".$name);
                        echo "<h2 align=\"center\">Successfully added!</h2>";
                }
                else{
                    print "<h3>Errors!</h3><ul><li>".join("</li><li>",$errors)."</li></ul>";
                }

2 个答案:

答案 0 :(得分:2)

Hi Markero M,

If you want to re-size images using PHP core function then you can use imagecopyresized and resizeImage function to re-size and you can get more information about these functions with example in php manual. please see below links all works great:
1) http://php.net/manual/en/function.imagecopyresized.php
2) http://php.net/manual/en/imagick.resizeimage.php
3)http://www.yetanothersite.com/php-function-to-resize-an-image.htm
4)http://www.developphp.com/view.php?tid=1140

And if you wish to use Classes for image re-resize you can refer:
1)http://tekbrand.com/php/resize-image-easily-using-php-image-resize-class
2)http://www.verot.net/php_class_upload.htm
3)https://github.com/nextone/PHP-Image-Resize
4)http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

答案 1 :(得分:-1)

在您共享的代码中,没有任何与文件大小调整相关的内容。您可以按照以下简单教程查看在php中上传时如何调整文件大小:

Upload and Resize an Image with PHP