繁体   English   中英

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

[英]Undefined index in first lines of thumbnail generation script

我正在使用一个脚本来生成缩略图,但是在第3行和第4行的第一行中遇到了错误。我猜测其中一个函数已被弃用(已经使用了一年),但是我真的没有理念。 GD支持已启用。 我正在阅读链接的问题并意识到我对isset没什么了解,但是我不确定如何为“图像”和“宽度”编写该信息,似乎也将在接下来的几篇中进行介绍线。 所有帮助表示赞赏。

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

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

<?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 ; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM