【问题标题】:reduce image size while uploading using the following PHP code used to upload image使用以下用于上传图像的 PHP 代码在上传时减小图像大小
【发布时间】:2014-11-06 03:53:09
【问题描述】:

我正在尝试使用以下代码将图像上传到服务器 以下是使用下面给定的代码完成的

  • 上传文件重命名
    我需要使用以下代码减小图像大小

这里是代码

 $file_name = $HTTP_POST_FILES['ufile']['name'];
 $random_digit = md5(rand() * time());
 $new_file_name = $random_digit . $file_name;
 $path = "upload/" . $new_file_name;
 if ($ufile != none) {
    if (copy($HTTP_POST_FILES['ufile']['tmp_name'], $path)) {
        echo "Successful<BR/>";
        echo "File Name :" . $new_file_name . "<BR/>";
        echo "File Size :" . $HTTP_POST_FILES['ufile']['size'] . "<BR/>";
        echo "File Type :" . $HTTP_POST_FILES['ufile']['type'] . "<BR/>";
    } else {
        echo "Error";
    }
 }

如何使用给定的代码减小图像大小

【问题讨论】:

    标签: php image uploading


    【解决方案1】:

    当然,它工作正常。我在 PHP 中使用这个类:

    <?php
    
    function thumbnail( $img, $source, $dest, $maxw, $maxh ) {      
        $jpg = $source.$img;
    
        if( $jpg ) {
            list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image
            $source = imagecreatefromjpeg( $jpg );
    
            if( $maxw >= $width && $maxh >= $height ) {
                $ratio = 1;
            }elseif( $width > $height ) {
                $ratio = $maxw / $width;
            }else {
                $ratio = $maxh / $height;
            }
    
            $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
            $thumb_height = round( $height * $ratio );
    
            $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
            imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );
    
            $path = $dest.$img."_thumb.jpg";
            imagejpeg( $thumb, $path, 75 );
        }
        imagedestroy( $thumb );
        imagedestroy( $source );
    }
    

    ?>

    imagejpeg( $thumb, $path, 75 );
    

    是上传后你想要多少图片的大小和质量。

    在 PHP 脚本中调用:

    if( isset( $_FILES['img-content'] ) ) {
    $img = str_replace( " ","_",$_FILES['img-content']['name'] );
    move_uploaded_file( $_FILES['img-content']['tmp_name'], "../../images/content/".$img );
    $source = "../../images/content/";
    $dest = "../../images/thumb/";
    thumbnail( $img, $source, $dest, 480, 400 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 2015-05-22
      相关资源
      最近更新 更多