2012-09-30 117 views
-1

我使用一个脚本,帮助用户上传自己的个人资料图片,然后裁剪他们选择合适的位置 我有他们未显示 这与PNG & GIF图像的问题作物文件代码请修改,以使其能够显示PNG,JPF和GIF图像显示裁剪图像

<?php 
sleep(1); 
$url = $_GET['url']; 
$type = $_GET['type']; 
if ($type='jpg') { 
    if(file_exists($url) AND preg_match('/^[a-z0-9\/_\-.]+$/i',$url)){ 
$width = (isset($_GET['width']) AND preg_match('/^[0-9]{2,}$/', $_GET['width'])) ? $_GET['width'] : 300; 
$height = (isset($_GET['height']) AND preg_match('/^[0-9]{2,}$/', $_GET['height'])) ? $_GET['height'] : 300; 
$left = (isset($_GET['left']) AND is_numeric($_GET['left'])) ? $_GET['left']  : 0; 
$top = (isset($_GET['top']) AND is_numeric($_GET['top']))  ? $_GET['top']  : 0; 
header ("Content-type: image/jpg"); 
$src = @imagecreatefromjpeg($url); 
$im  = @imagecreatetruecolor($width, $height); 
imagecopy($im,$src,0,0,-$left,-$top,$width,$height); 
imagejpeg($im,"",300); 
imagedestroy($im);}} 

    elseif ($type='png') { 

    if(file_exists($url) AND preg_match('/^[a-z0-9\/_\-.]+$/i',$url)){ 
$width = (isset($_GET['width']) AND preg_match('/^[0-9]{2,}$/', $_GET['width'])) ? $_GET['width'] : 300; 
$height = (isset($_GET['height']) AND preg_match('/^[0-9]{2,}$/', $_GET['height'])) ? $_GET['height'] : 300; 
$left = (isset($_GET['left']) AND is_numeric($_GET['left'])) ? $_GET['left']  : 0; 
$top = (isset($_GET['top']) AND is_numeric($_GET['top']))  ? $_GET['top']  : 0; 
header ("Content-type: image/png"); 
$src = @imagecreatefrompng($url); 
$im  = @imagecreatetruecolor($width, $height); 
imagecopy($im,$src,0,0,-$left,-$top,$width,$height); 
imagepng($im,"",300); 
imagedestroy($im); 
    } 
     } 
    ?> 
+0

请查看'filter_var'并采用适当的编码风格。 – Gordon

回答

0

你必须检查输入文件的格式,然后调用支持的功能。

如果输入文件是jpeg,请拨打imagecreatefromjpegimagejpeg
如果输入文件是PNG,请致电imagecreatefrompngimagepng
如果输入文件是gif,请拨打imagecreatefromjpegimagegif

+1

或者从图像数据中使用'imagecreatefromstring()',并在确定其类型后使用'imageTYPE()'函数之一。 –

+0

不起作用 我加了$ type = $ _GET ['type'];和elseif($ type ='png') –

+0

$ _GET ['type']的值是多少?你是否100%确定它是正确的?另外,你应该使用'switch(){}'而不是巨大的'elseif'块。 – Darkwater