【问题标题】:How to rotate an image before uploading, rotation not working as a variable如何在上传之前旋转图像,旋转不能作为变量工作
【发布时间】:2017-12-09 04:53:06
【问题描述】:

当在文本中定义旋转度数(例如“-90”)时,此函数非常有效,但是当我尝试将度数参数作为 $variable 时,图像不会旋转。有任何想法吗?提前致谢。

 function compress_image($source_url, $destination_url, $quality) {

    $info = getimagesize($source_url);

        if ($info['mime'] == 'image/jpeg')  
                $image = imagecreatefromjpeg($source_url);

        elseif ($info['mime'] == 'image/gif')
                $image = imagecreatefromgif($source_url);

    elseif ($info['mime'] == 'image/png')
                $image = imagecreatefrompng($source_url);


                $rotate = imagerotate($image,"-90",0); //Does Work
                $degrees = "-90"
                $rotate = imagerotate($image,$degrees,0); //Does Not Work
                $image = $rotate;

        imagejpeg($image, $destination_url, $quality);
    return $destination_url;
}

【问题讨论】:

    标签: php image


    【解决方案1】:

    你可以如下旋转:

    resource imagerotate ( resource $image , float $angle , int $bgd_color [, int $ignore_transparent = 0 ] )
    

    使用给定的角度旋转图像图像。

    旋转中心是图像的中心,旋转后的图像可能与原始图像有不同的尺寸。

    参数:-

    图片

    图像资源,由图像创建函数之一返回,例如 imagecreatetruecolor()。

    角度

    旋转角度,以度为单位。旋转角度被解释为逆时针旋转图像的度数。

    bgd_color

    指定旋转后未覆盖区域的颜色

    ignore_transparent

    如果设置且非零,则忽略透明颜色(否则保留)。

    示例

    <?php
     // File and rotation
     $filename = 'test.jpg';
     $degrees = 180;
    
     // Content type
     header('Content-type: image/jpeg');
    
     // Load
     $source = imagecreatefromjpeg($filename);
    
     // Rotate
     $rotate = imagerotate($source, $degrees, 0);
    
     // Output
     imagejpeg($rotate);
    
     // Free the memory
     imagedestroy($source);
     imagedestroy($rotate);
    ?>
    

    【讨论】:

    • 好的,阿莫尔,我明白你的意思了。 $degrees="-90" 不起作用,但 $degrees=90 起作用。话虽如此,我在另一个页面上以表格形式发布 name="degrees",然后在我的 php 脚本页面上发布 $degrees=$_REQUEST["degrees"] 。那么我对变量类型有疑问。如何使我的 $degrees 变量成为 imagerotate 命令的有效类型
    • 所有旋转角度都在0到360之间。
    • 尝试了积极的 90 和 180,但仍然没有。这很奇怪,如果我对度数进行硬编码,它会起作用,但不会让我分配给变量
    • 我也试过 $degress = intval($degrees);没有去
    【解决方案2】:

    我试过你的代码。您的代码完全可以正常工作,除非在以下位置出现错误:

    $degrees = "-90"
    

    应该是:

    $degrees = "-90";
    

    希望对你有帮助。

    【讨论】:

    • 对不起菲尔,我只是使用 $degrees = "90" 作为示例目的,是的,我看到了错字。但实际上,正如我在上一条评论中提到的那样,我从不同的页面传递了 $degrees。
    • 那很奇怪。也许你可以回应你的学位值并检查会发生什么。
    【解决方案3】:

    我没有将 $degree 变量传递给函数。下面的代码是正确的。

    function compress_image($source_url, $destination_url, $quality, $degree) {
    
    $info = getimagesize($source_url);
    
        if ($info['mime'] == 'image/jpeg')  
                $image = imagecreatefromjpeg($source_url);
    
        elseif ($info['mime'] == 'image/gif')
                $image = imagecreatefromgif($source_url);
    
    elseif ($info['mime'] == 'image/png')
                $image = imagecreatefrompng($source_url);
    
                $rotate = imagerotate($image,$degrees,0);
                $image = $rotate;
    
        imagejpeg($image, $destination_url, $quality);
    return $destination_url;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-17
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多