2015-05-18 121 views
0

我有这个代码,我想调整图片大小,它设法做第一个100x100而不是其他人。我不明白为什么它不起作用,它没有任何错误。我是一个PHP新手,并不真正知道哪里出了问题,但正如你所看到的,代码对第一个代码有用,但为什么不能为其他人工作?图像不显示

<?php 

function thumbnail($image, $width, $height) { 

    $image_properties = getimagesize($image); 
    $image_width = $image_properties[0]; 
    $image_height = $image_properties[1]; 
    $image_ratio = $image_width/$image_height; 
    $type = $image_properties["mime"]; 

    if(!$width && !$height) { 
     $width = $image_width; 
     $height = $image_height; 
    } 
    if(!$width) { 
     $width = round($height * $image_ratio); 
    } 
    if(!$height) { 
     $height = round($width/$image_ratio); 
    } 


    if($type == "image/jpeg") { 
     header('Content-type: image/jpeg'); 
     $thumb = imagecreatefromjpeg($image); 
    } elseif($type == "image/png") { 
     header('Content-type: image/png'); 
     $thumb = imagecreatefrompng($image); 
    } elseif($type == "image/gif") { 
     header('Content-type: image/gif'); 
     $thumb = imagecreatefromgif($image); 
    } 
    else { 
     return false; 
    } 

    $temp_image = imagecreatetruecolor($width, $height); 
    imagecopyresampled($temp_image, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height); 
    $thumbnail = imagecreatetruecolor($width, $height); 
    imagecopyresampled($thumbnail, $temp_image, 0, 0, 0, 0, $width, $height, $width, $height); 

    if($type == "image/jpeg") { 
     imagejpeg($thumbnail); 
    } 
    elseif($type == "image/jpeg") { 
     imagepng($thumbnail); 
    } 
    elseif($type == "image/gif") { 
     imagegif($thumbnail); 
    } 

    imagedestroy($temp_image); 
    imagedestroy($thumbnail); 


} 

$pic_size = array(); 

// Adjust size 

$pic_size['height'][0] = 100; 
$pic_size['width'][0] = 100; 

$pic_size['height'][1] = 200; 
$pic_size['width'][1] = 200; 

$pic_size['height'][2] = 300; 
$pic_size['width'][2] = 300; 

$pic_size['height'][3] = 400; 
$pic_size['width'][3] = 400; 

$pic_size['height'][4] = 500; 
$pic_size['width'][4] = 500; 

$total_pic_size= count($pic_size['height']); 

    $x = 0; 

    foreach(array_keys($pic_size['height']) as $x) { 
     thumbnail($_GET["img"], $pic_size['width'][$x], $pic_size['height'][$x]); 
     echo '<img src="index.php?w='.$pic_size['width'][$x].'&h='.$pic_size['height'][$x].'&img='.$_GET["img"].'" />'; 
     $x++; 
    } 



?> 
+2

在打开PHP标记后立即在文件顶部添加错误报告,例如'<?php error_reporting(E_ALL); ini_set('display_errors',1); '然后你的代码的其余部分,看看它是否产生任何东西。 –

+0

@PedroLobito我做了但没有报告错误。我甚至检查了php错误日志,但没有任何错误。 – MuthaFury

+0

您创建图像,但从不保存到服务器,函数没有返回值。 'count($ total_pic_size)'返回2,但现在没关系。 – panther

回答

0

你只重复两次,因为

$total_pic_size= count($pic_size);// is 2 

修改成:

$total_pic_size= count($pic_size['height']); 

或者使用的foreach:

foreach(array_keys($pic_size['height']) as $x) 

而且不要在这里使用COUNT_RECURSIVE ,它会返回w rong号码,计数在widthheight键。

另外thumbnail函数有header调用它,在你的循环中调用echo,在你的情况下,它是在echo之后调用的。你应该看到“Headers already sent”的警告。

您可以将图像保存为文件并以文件路径作为源回显图像。取出header通话,让你thumbnail功能保存并返回的文件名,例如:

imagejpeg($thumbnail, $filename); 
return $filename; 

,并使用输出作为呼应图像的src

+0

很好,赶上!我已更新计数递归。 – MuthaFury

+0

更新了代码,你可以检查:) – MuthaFury

+0

谢谢!我想我从这里得到它。 – MuthaFury

0

$total_pic_size总是2

$total_pic_size = count($pic_size['width']); 

这会给你正确数量的项目,循环不应只运行一次。

你也应该改变循环条件:

while ($x <= $total_pic_size) { 

#UPDATE 1

你可以尝试这样的事情:

// ... 

$pic_sizes[0]['height'] = 100; 
$pic_sizes[0]['width'] = 100; 

$pic_sizes[1]['height'] = 200; 
$pic_sizes[1]['width'] = 200; 

$pic_sizes[2]['height'] = 300; 
$pic_sizes[2]['width'] = 300; 

$pic_sizes[3]['height'] = 400; 
$pic_sizes[3]['width'] = 400; 

$pic_sizes[4]['height'] = 500; 
$pic_sizes[4]['width'] = 500; 

$img = $_GET["img"]; 
foreach ($pic_sizes as $pic_size) { 
    thumbnail($img, $pic_size['width'], $pic_size['height']); 
    echo '<img src="index.php?w='.$pic_size['width'].'&h='.$pic_size['height'].'&img='.$img.'" />'; 
} 

#UPDATE 2:

您已经接受了另一个回答,但也许这对您也有帮助。我在代码中发现了一些更多的错误和逻辑问题...下面的代码工作(没有保存大拇指)。

<?php 

function thumbnail($image, $width, $height) { 
    $image_properties = getimagesize($image); 
    $image_width = $image_properties[0]; 
    $image_height = $image_properties[1]; 
    $image_ratio = $image_width/$image_height; 
    $type = $image_properties["mime"]; 

    if(!$width && !$height) { 
     $width = $image_width; 
     $height = $image_height; 
    } 
    if(!$width) { 
     $width = round($height * $image_ratio); 
    } 
    if(!$height) { 
     $height = round($width/$image_ratio); 
    } 


    if($type == "image/jpeg") { 
     header('Content-type: image/jpeg'); 
     $thumb = imagecreatefromjpeg($image); 
    } elseif($type == "image/png") { 
     header('Content-type: image/png'); 
     $thumb = imagecreatefrompng($image); 
    } elseif($type == "image/gif") { 
     header('Content-type: image/gif'); 
     $thumb = imagecreatefromgif($image); 
    } 
    else { 
     return false; 
    } 

    $temp_image = imagecreatetruecolor($width, $height); 
    imagecopyresampled($temp_image, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height); 
    $thumbnail = imagecreatetruecolor($width, $height); 
    imagecopyresampled($thumbnail, $temp_image, 0, 0, 0, 0, $width, $height, $width, $height); 

    if($type == "image/jpeg") { 
     imagejpeg($thumbnail); 
    } 
    elseif($type == "image/png") { 
     imagepng($thumbnail); 
    } 
    elseif($type == "image/gif") { 
     imagegif($thumbnail); 
    } 

    imagedestroy($temp_image); 
    imagedestroy($thumbnail); 
} 

$img = $_GET["img"]; 

$gen = @$_GET["gen"]; 
$w = @$_GET["w"]; 
$h = @$_GET["h"]; 
if (!$gen) { 
    $pic_sizes = array(); 

    // Adjust size 
    $pic_sizes[0]['height'] = 100; 
    $pic_sizes[0]['width'] = 100; 

    $pic_sizes[1]['height'] = 200; 
    $pic_sizes[1]['width'] = 200; 

    $pic_sizes[2]['height'] = 300; 
    $pic_sizes[2]['width'] = 300; 

    $pic_sizes[3]['height'] = 400; 
    $pic_sizes[3]['width'] = 400; 

    $pic_sizes[4]['height'] = 500; 
    $pic_sizes[4]['width'] = 500; 

    foreach ($pic_sizes as $pic_size) { 
     echo '<img src="'.$_SERVER['PHP_SELF'].'?w='.$pic_size['width'].'&h='.$pic_size['height'].'&img='.$img.'&gen=1" />'; 
    } 
} else { 
    thumbnail($img, $w, $h); 
} 
+0

好抓住那里!我已更新计数递归。 – MuthaFury

+0

COUNT_RECURSIVE(10而不是5 !!)你的人数太高。 – Jan

+0

更新了我的代码,但仍然没有解决问题。 – MuthaFury