调整上传php的图像 [英] Resize image on upload php

查看:126
本文介绍了调整上传php的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <?php 
$ LibID = $ _POST [名称];
define(MAX_SIZE,10000);
函数getExtension($ str){
$ i = strrpos($ str,。);
if(!$ i){return; }
$ l = strlen($ str) - $ i;
$ ext = substr($ str,$ i + 1,$ l);
return $ ext;
}
$ errors = 0;
$ image = $ _ FILES ['image'] ['name'];
if($ image)
{
$ filename = stripslashes($ _ FILES ['image'] ['name']);
$ extension = getExtension($ filename);
$ extension = strtolower($ extension); (($ extension!=jpg)&&($ extension!=jpeg))
{
echo'< h1>未知的扩展程序!
< H1>';
$ errors = 1;
exit();
}
else
{
$ size = filesize($ _ FILES ['image'] ['tmp_name']);
if($ size> MAX_SIZE * 1024)
{
echo'< h1>您已经超出尺寸限制< / h1>';
$ errors = 1;
exit();
}
$ image_name = $ LibID。'。'。$ extension;
$ newname =uimages /\".$ image_name;
$ copied = copy($ _ FILES ['image'] ['tmp_name'],$ newname);
if(!$ copied)
{
echo'< h1>图片上传不成功!< / h1>';
$ errors = 1;
exit();
}}}
?>

将图像文件上传到根目录中的文件夹uimages。我已经通过定义最大高度和最大宽度来改变图像压缩显示的html文件。但是我想调整上传的图像文件。图像文件的最大宽度为100px,最大高度为150px。图像比例必须受到限制。也就是说,图像可能比上面的尺寸要小,但不应该超出限制。我怎么能做到这一点?



预先感谢:)

blasteralfred ..

解决方案

请参阅这个答案,我喜欢使用imagemagick


I have a php script for image upload as below

<?php
$LibID = $_POST[name];
 define ("MAX_SIZE","10000");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 $errors=0;
    $image=$_FILES['image']['name'];
    if ($image)
    {
        $filename = stripslashes($_FILES['image']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
 if (($extension != "jpg") && ($extension != "jpeg"))
        {
            echo '<h1>Unknown extension!</h1>';
            $errors=1;
            exit();
        }
        else
        {
 $size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
    echo '<h1>You have exceeded the size limit!</h1>';
    $errors=1;
    exit();
}
$image_name=$LibID.'.'.$extension;
$newname="uimages/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
    echo '<h1>image upload unsuccessfull!</h1>';
    $errors=1;
    exit();
}}}
?>

which uploads the image file to a folder "uimages" in the root. I have made changes in the html file for the compact display of the image by defining "max-height" and "max-width". But i want to resize the image file on upload. The image file may have a maximum width of 100px and maximum height of 150px. The image proportions must be constrained. That is, the image may be smaller than the above dimensions, but, it should not exceed the limit. How can I make this possible??

Thanks in advance :)

blasteralfred..

解决方案

See this answer, I like myself using imagemagick

这篇关于调整上传php的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆