CloudFlare 运行时 PHP 代码不起作用?

Posted

技术标签:

【中文标题】CloudFlare 运行时 PHP 代码不起作用?【英文标题】:PHP Code Not Working when CloudFlare is running? 【发布时间】:2018-08-01 21:33:26 【问题描述】:

下面的代码可以完美地裁剪已上传的图像(通过 JQuery JCrop)。一旦我配置了 CloudFlare,它就无法裁剪图像(但仍然可以成功上传)。有关如何解决此问题的任何想法?

我当前的 php 版本是 7.0,我使用 DigitalOcean 作为我的虚拟主机。

<?php

require_once('auth.php');
require_once('connection.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST')

 $targ_w = 150;
 $targ_h = 200;


 $src = mysqli_real_escape_string($bd,$_POST['src']);

 $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

 $img = resize_image($src, $_POST['imgWidth'], $_POST['imgHeight']);

 imagecopyresampled($dst_r,$img,0,0,$_POST['x'],$_POST['y'],150,200,$_POST['w'],$_POST['h']);

 imagejpeg($dst_r,$src,90);


 //Cropping complete, move to next page

 //header("location: profileSetup.php");



 exit; 
 


function isPNG($file)
    return preg_match('/'.quotemeta('PNG').'/i', file_get_contents($file));




function resize_image($file, $w, $h, $crop=FALSE) 
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) 
        if ($width > $height) 
            $width = ceil($width-($width*abs($r-$w/$h)));
         else 
            $height = ceil($height-($height*abs($r-$w/$h)));
        
        $newwidth = $w;
        $newheight = $h;
     else 
        if ($w/$h > $r) 
            $newwidth = $h*$r;
            $newheight = $h;
         else 
            $newheight = $w/$r;
            $newwidth = $w;
        
    



    $dst = imagecreatetruecolor($newwidth, $newheight);

    if(exif_imagetype($file) != IMAGETYPE_JPEG)
        $src = imagecreatefrompng($file);
    
    else
        $src = imagecreatefromjpeg($file);
    



    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;



?>

【问题讨论】:

【参考方案1】:

我终于自己解决了这个问题。 PHP 代码没有任何问题。 CloudFlare 正在缓存图像,我所要做的就是在新图像名称中添加一个随机整数以防止这种情况发生!

【讨论】:

以上是关于CloudFlare 运行时 PHP 代码不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

PHP file_get_contents 函数无法使用 Cloudflare(网络服务器代理)

require_once 在 php 5.3.0 中不起作用 [关闭]

带有 CloudFlare 的子域

在计算引擎上从 PHP 的 exec() 调用时,gsutil 不起作用

.BLADE.PHP 在我的 LARAVEL 项目中不起作用。 .PHP 在我看来运行良好

php中的cURL不起作用