缩略图生成脚本第一行中未定义的索引


Undefined index in first lines of thumbnail generation script

我正在使用我找到的脚本来生成缩略图,但是在第 3 行和第 4 行的第一个中出现错误。我猜其中一个函数已被弃用(它已经有一年的历史了),但我真的不知道。GD 支持已启用。我正在阅读链接的问题,并意识到有些东西我没有isset,但我不确定如何为"图像"和"宽度"写这个,它似乎也设置在接下来的几行中。所有的帮助都表示赞赏。

注意:未定义的索引:图像在 C:''xampp''htdocs''thumbnail''thumbnail.php 在第 3 行

注意:未定义的索引:C:''xampp''htdocs''thumbnail''thumbnail.php 第 4 行的宽度

<?php
$imageSrc = (string)$_GET['image'];    
$width = $_GET['width']; 
if (is_numeric($width) && isset($imageSrc)){ 
    header('Content-type: image/jpeg');
    makeThumb($imageSrc, $width); 
}
function makeThumb($src,$newWidth) { 
    // read the source image given 
    $srcImage = imagecreatefromjpeg($src); 
    $width = imagesx($srcImage); 
    $height = imagesy($srcImage); 
    // find the height of the thumb based on the width given 
    $newHeight = floor($height*($newWidth/$width)); 
    // create a new blank image 
    $newImage = imagecreatetruecolor($newWidth,$newHeight);
     // copy source image to a new size 
     imagecopyresized($newImage,$srcImage,0,0,0,0,$newWidth,$newHeight,$width,$height);
     // create the thumbnail 
     imagejpeg($newImage); 
} 
?>

我意识到为每个页面加载动态生成脚本效率不高,但我只是想让一些东西起作用。

我进行了劳伦斯建议的第三次更改,但仍然收到错误:

注意:未定义的变量:宽度在 C:''xampp''htdocs''thumbnail''thumbnail.php 在第 13 行

在使用之前,您需要检查那里的设置:

改变:

$imageSrc = (string)$_GET['image'];    
$width = $_GET['width']; 

$imageSrc = (isset($_GET['image']))?$_GET['image']:null;    
$width =  (isset($_GET['width']))?$_GET['width']:null;

或者如果其他方式

if(isset($_GET['image'])){$imageSrc = $_GET['image'];}else{$imageSrc =null;}
if(isset($_GET['width'])){$width = $_GET['width'];}else{$width =null;}

或者你可以忘记 2 行,只做:

if (isset($_GET['width']) && is_numeric($_GET['width']) && isset($_GET['image'])){ 
    header('Content-type: image/jpeg');
    makeThumb(basename($_GET['image']), $_GET['width']); 
}

使用isset

尝试

$imageSrc = isset($_GET['image']) ? $_GET['image'] : null;    
$width = isset($_GET['width']) ? $_GET['width'] : null ;