douchun1961 2015-03-30 00:30
浏览 6
已采纳

PHP映像在上载到指定目录之前调整大小

Hi I'm developing a site wherein a user can upload his/her profile picture. My upload page has an img tag so that a user can preview what their image would look like before they upload it after they have selected this picture the thing is the image's name would be fetched from the database later on. here is my the snippet for it

 <img id="imgs" src="http://placehold.it/200x200" alt="your image"/>

my uploader goes something like this

if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
{
    $info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file
    $temp = explode(".",$_FILES["file"]["name"]);
    $filename = rand(1,9999) . '.' .end($temp);
    if ( in_array( end($info), $allow) ) // is this file allowed
    {
        if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir .  $filename ) )
        {

        }
    }
    else
    { }
}

by the way this uploader script is from a tutorial so yeah you might find several security and other issues here..

What I'm having trouble is the image looks fine when I uploaded an image with a dimension of 200x200 because in my css I have defined imgs with a width and height set both at 200px but if I uploaded other images that doesnt meet the the width and height that I have set on my css the image turns into stretched if its bigger

Any ideas out there how can I fix this? I also tried defining sizes on the img directly and turned out the same obviously can I fix this with just css alone?

  • 写回答

5条回答 默认 最新

  • douzhang3356 2015-03-30 01:17
    关注

    Here is a simple tutorial about image resizing at https://phpmatters.com/resize-image-using-php/

    Here is a sample snippet of how you could resize your user images. There are several intelligent options. PHP resizing is smarter than allowing users to insert huge images in the HTML page (the CSS-only sizing method).

    For example, if you want to resize to a specifed width (e.g. 200px) but keep the dimensions ratio the same then the script can work out the required height for you, just use the resizeToWidth function.

    <?php
        include('SimpleImage.php');
        $image = new SimpleImage();
        $image->load('picture.jpg');
        $image->resizeToWidth(200);
        $image->save('picture200x200.jpg');
    ?>
    

    On the PHP size, you can also reject images smaller than 200x200 so you don't have to worry about unacceptably small images being saved.

    ...this plugin is no longer supported due to security exploits

    FWIW, if you have access to the .htaccess file on your server, adding this will deny external access to your timthumb.php. It's such a great library if used internally.

    # Prevent 'timthumb' hack attempt
    RewriteRule ^(.*)?(timthumb\.php)$ - [F]
    

    EDIT: An alternative library to TimThumb is the WideImage library that has many features at http://wideimage.sourceforge.net/examples/resizing/

    Here is a thread on SO about using it here: Keeping aspect ration when resizing images using WideImage PHP library

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像