0

我是 php 新手,我已经下载了图片库的免费源代码。它有管理员帐户,您可以在其上上传图像。现在,当我上传图片时,它会生成随机图片标题,如数字“4849404”。我希望它为每个图像分配一个特定的标题。我怎样才能做到这一点?如果我没记错的话,我认为这是图像标题的行:

$RandomNumber   = rand(0, 9999999999); // We need same random name for both files.

// some information about image we need later.
$ImageName      = strtolower($_FILES['ImageFile']['name']);
$ImageSize      = $_FILES['ImageFile']['size']; 
$TempSrc        = $_FILES['ImageFile']['tmp_name'];
$ImageType      = $_FILES['ImageFile']['type'];
$process        = true;



//Validate file + create image from uploaded file.
switch(strtolower($ImageType))
{
    case 'image/png':
        $CreatedImage = imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
        break;      
    case 'image/gif':
        $CreatedImage = imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
        break;
    case 'image/jpeg':
        $CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
        break;
    default:
        die('Unsupported File!'); //output error
}

//get Image Size
list($CurWidth,$CurHeight)=getimagesize($TempSrc);

//get file extension, this will be added after random name
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$BigImageMaxWidth       = $CurWidth;
$BigImageMaxHeight      = $CurHeight;

//Set the Destination Image path with Random Name
$thumb_DestRandImageName    = $Thumb.$RandomNumber.'.'.$ImageExt; //Thumb name
$DestRandImageName          = $DestinationDirectory.$RandomNumber.'.'.$ImageExt; //Name for Big Image

//Resize image to our Specified Size by calling our resizeImage function.
if(resizeImage($CurWidth,$CurHeight,$BigImageMaxWidth,$BigImageMaxHeight,$DestRandImageName,$CreatedImage))
{
    //Create Thumbnail for the Image
    resizeImage($CurWidth,$CurHeight,$ThumbMaxWidth,$ThumbMaxHeight,$thumb_DestRandImageName,$CreatedImage);

    //respond with our images
    echo '<table width="100%" border="0" cellpadding="4" cellspacing="0">
        <tr><td align="center"><img src="gallery/'.$RandomNumber.'.'.$ImageExt.'" alt="Resized Image"></td></tr></table>';

}else{
    die('Resize Error'); //output error
}

}

4

3 回答 3

0

您发布的代码片段似乎正在创建和命名thumbnails,我们可以从评论和变量名称中注意到:

//Set the Destination Image path with Random Name
$thumb_DestRandImageName    = $Thumb.$RandomNumber.'.'.$ImageExt; //Thumb name

并从以下几行中确认其用法:

//Create Thumbnail for the Image
resizeImage($CurWidth,$CurHeight,$ThumbMaxWidth,$ThumbMaxHeight,$thumb_DestRandImageName,$CreatedImage);

阅读$_FILES数组 - nametypesize、的内容tmp_nameerror了解可用的详细信息。例如在上面的代码片段中,

$ImageName      = strtolower($_FILES['ImageFile']['name']);

获取小写的原始文件名(因为strtolower),我们可以按照我们认为合适的方式使用它,例如使用它,或者在为主名称添加一些随机字符串之后。此外,您可能已经注意到,文件扩展名也必须单独管理。

最后,如果您要重命名可能正在使用的最初上传的文件move_uploaded_file,请挖掘代码并以类似的方式进行测试。

于 2013-06-03T10:41:09.110 回答
0

它在以下几行中为标题添加了一个随机数:

$DestRandImageName = $DestinationDirectory.$RandomNumber.'.'.$ImageExt;

$DestRandImageName = $DestinationDirectory.$RandomNumber.'.'.$ImageExt;

如果它说 $RandomNumber 您可以在此处添加设置名称的功能,例如将上述代码放入一个方法中并传递一个设置名称的变量,或者您可以从以下变量中检索现有图像名称:

$ImageName
于 2013-06-03T10:22:14.260 回答
0

$thumb_DestRandImageName = $Thumb.$RandomNumber.'.'.$ImageExt; //拇指名称 $DestRandImageName = $DestinationDirectory.$RandomNumber.'.'.$ImageExt;

此行是您可以添加特定标题的地方。$RandomNumber="标题";

于 2013-06-03T14:08:57.790 回答