doulan2827 2014-10-24 18:52
浏览 16

“FancyResize”PHP脚本 - 破坏图像

I have a script from an old website called FancyResize, that resizes images to a new requested width and height. For some reason it now "destroys" images when used - not every time, but in many circumstances it makes a weird color overlay, changes pixels etc., so it is impossible to see what image it actually should have been. I guess it could have something to do with new PHP version etc., but I don't know, but I guess it have worked before.

So, my question is: can anybody see a mistake in this?

<?
$new_width = $_REQUEST[w];
$new_height = $_REQUEST[h];
$file = $_REQUEST[i];


$localpath = "WEBSITENAME";

$query = $_SERVER[QUERY_STRING];    
$datafile = $_SERVER[DOCUMENT_ROOT].'/img/fronttemp/'.md5($query);

if (file_exists($datafile))
{
   header("Content-type: image/jpeg");
   die(file_get_contents($datafile));
}

if (substr($file, 0, strlen($localpath)) == $localpath)
{
    $file = substr($file, strlen($localpath));
    $file = $_SERVER[DOCUMENT_ROOT].$file;
}
else
{

    $file = substr($query, strpos($query, "&i=")+3);
}

$image = imagecreatefromjpeg($file);
if (!$image) 
{
$image =imagecreatefromgif($file);
$is_gif = true;
}
else $is_gif = false;

if (!$image) die("error reading gif/jpg $file");

$info = getimagesize($file) or die("error getting info");

$width = $info[0];
$height = $info[1];

$input_landscape = ($width/$height)>1;
$output_landscape = ($new_width/$new_height)>1;




function imageScaleToWidth($image, $new_height, $new_width)
{
 $width = imagesx($image);
 $height = imagesy($image);

$scale_height = ceil(($new_width/$width)*$height);
$scaledimage = imagecreatetruecolor($new_width, $scale_height);

$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);

imagecopyresampled($scaledimage, $image, 0,0,0,0, $new_width, $scale_height, $width, $height);
return $scaledimage;
 }

function imageScaleToHeight($image, $new_height, $new_width)
{
 $width = imagesx($image);
 $height = imagesy($image);

$scale_width = ceil(($new_height/$height)*$width);
$scaledimage = imagecreatetruecolor($scale_width, $new_height);

$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);


imagecopyresampled($scaledimage, $image, 0,0,0,0, $scale_width, $new_height, $width, $height);
    return $scaledimage;
  }


function makeImageWideEnough($image, $new_height, $new_width)
{
 $width = imagesx($image);
 $height = imagesy($image);

 if ($width < $new_width)
 {
$scale_height = ceil(($new_width/$width)*$height);
$scaledimage = imagecreatetruecolor($new_width, $scale_height);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $new_width, $scale_height, $width, $height);
 return $scaledimage;
  }

 return $image;
  }

 function makeImageHighEnough($image, $new_height, $new_width)
 {
 $width = imagesx($image);
 $height = imagesy($image);

 if ($height < $new_height)
 {
$scale_width = ceil(($new_height/$height)*$width);
    $scaledimage = imagecreatetruecolor($scale_width, $new_height);
    imagecopyresampled($scaledimage, $image, 0,0,0,0, $scale_width, $new_height, $width, $height);
 return $scaledimage;
}

 return $image;
}

function imageCrop($image, $new_height, $new_width)
{
 $width = imagesx($image);
 $height = imagesy($image);

$croppedimage = imagecreatetruecolor($new_width, $new_height);

$sx = ($width-$new_width)/4;
$sy = ($height-$new_height)/4;
imagecopy($croppedimage, $image, 0, 0, $sx, $sy, $new_width, $new_height);
return $croppedimage;
}

function scaleWidth($image, $new_width)
{
 $width = imagesx($image);
 $height = imagesy($image);

 $ratio = $new_width / $width;

 $sx = ceil($width*$ratio);
 $sy = ceil($height*$ratio);


$scaledimage = imagecreatetruecolor($sx, $sy);

$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);


imagecopyresampled($scaledimage, $image, 0,0,0,0, $sx, $sy, $width, $height);
return $scaledimage;
 }


$croppedimage = scaleWidth($image, $new_width);

if ($new_height < imagesy($croppedimage))
{
$croppedimage = imageCrop($croppedimage,$new_height,$new_width);
 }

header("Content-type: image/jpeg");
imagejpeg($croppedimage, $datafile);
echo file_get_contents($datafile);  
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题