“FancyResize”PHP脚本 - 破坏图像

时间:2014-10-24 18:52:34

标签: php image resize

我有一个名为FancyResize的旧网站的脚本,它将图像大小调整为新请求的宽度和高度。由于某种原因,它现在"摧毁"使用时的图像 - 不是每次都有,但在许多情况下,它会产生奇怪的颜色叠加,更改像素等,因此无法看到它实际上应该是什么图像。我想它可能与新的PHP版本等有关,但我不知道,但我想它以前有用过。

所以,我的问题是:任何人都可以看到这个错误吗?

<?
$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 个答案:

没有答案