如何在 PHP 中保存不同名称的相同图像?
How to save same image with different names in PHP?
我已经编写了一个从 .jpg 图像创建缩略图的函数。但我想做的是,无论何时调用该函数,相同的图像都应在同一目的地保存为 1.jpg、2.jpg、3.jpg。
我使用了会话和静态变量概念,但没有成功。
这是我的代码。
这是thumbsave.php文件
<?php
session_start();
$_SESSION['sid']=$k=1;
function createThumb($fpath)//fpath will be passed here as parameter.
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims),
imagesy($ims));
imagejpeg($imd,"saveimages/" . $_SESSION['sid'] . ".jpg");
$_SESSION['sid'] = $_SESSION['sid'] + 1;
imagedestroy($ims);
imagedestroy($imd);
echo "Thumbnail Created and Saved at the Destination";
}
?>
这是我的 dynamicthumb.php
代码
<?php
include("include/thumbsave.php");
createThumb("imgs/m1.jpeg");
?>
所以,当我运行 dynamicthumb.php 文件时,存储在 imgs 文件夹中的图像必须存储在 saveimages 文件夹中。但是这里只保存了1张图片,并没有像2.jpg,3.jpg.
那样生成多份
这是负责将图像保存到文件的行:
imagejpeg($imd,"saveimages/" . $_SESSION['sid'] . ".jpg");
您可以将其更新为使用 $fpath
而不是 ,$_SESSION['sid']
:
imagejpeg($imd, $fpath);
但请注意,您的路径应以 .jpg.
结尾
如果您确定缩略图的名称将始终为数字,则可以使用 file_exists 循环:
function createThumb($fpath)
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), imagesy($ims));
$thumb = 1;
while ( file_exists('saveimages/'. $thumb .'.jpg') ) { $thumb++; }
imagejpeg($imd,'saveimages/'. $thumb .'.jpg');
imagedestroy($ims);
imagedestroy($imd);
echo 'Thumbnail Created and Saved at the Destination as '. $thumb .'.jpg';
}
但是我质疑你所要求的背后的逻辑...因为这表明你创建的每个缩略图都只是一个序列号,都存储在同一个目录中?
使用 counter
数据库字段或文件可能会更幸运,因为对包含数千个且还在不断增长的文件夹执行 file_exists
可能会影响性能。
因此,基于文件的 counter
解决方案可能是:
function createThumb($fpath)
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), imagesy($ims));
$thumb = file_get_contents('saveimages/thumbcount.txt');
$thumb++; file_put_contents('saveimages/thumbcount.txt',$thumb);
imagejpeg($imd,'saveimages/'. $thumb .'.jpg');
imagedestroy($ims);
imagedestroy($imd);
echo 'Thumbnail Created and Saved at the Destination as '. $thumb .'.jpg';
}
一定要用 1
填充 thumbcount.txt
,这样它就有了开头。
我已经编写了一个从 .jpg 图像创建缩略图的函数。但我想做的是,无论何时调用该函数,相同的图像都应在同一目的地保存为 1.jpg、2.jpg、3.jpg。
我使用了会话和静态变量概念,但没有成功。
这是我的代码。 这是thumbsave.php文件
<?php
session_start();
$_SESSION['sid']=$k=1;
function createThumb($fpath)//fpath will be passed here as parameter.
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims),
imagesy($ims));
imagejpeg($imd,"saveimages/" . $_SESSION['sid'] . ".jpg");
$_SESSION['sid'] = $_SESSION['sid'] + 1;
imagedestroy($ims);
imagedestroy($imd);
echo "Thumbnail Created and Saved at the Destination";
}
?>
这是我的 dynamicthumb.php
代码<?php
include("include/thumbsave.php");
createThumb("imgs/m1.jpeg");
?>
所以,当我运行 dynamicthumb.php 文件时,存储在 imgs 文件夹中的图像必须存储在 saveimages 文件夹中。但是这里只保存了1张图片,并没有像2.jpg,3.jpg.
那样生成多份这是负责将图像保存到文件的行:
imagejpeg($imd,"saveimages/" . $_SESSION['sid'] . ".jpg");
您可以将其更新为使用 $fpath
而不是 ,$_SESSION['sid']
:
imagejpeg($imd, $fpath);
但请注意,您的路径应以 .jpg.
结尾如果您确定缩略图的名称将始终为数字,则可以使用 file_exists 循环:
function createThumb($fpath)
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), imagesy($ims));
$thumb = 1;
while ( file_exists('saveimages/'. $thumb .'.jpg') ) { $thumb++; }
imagejpeg($imd,'saveimages/'. $thumb .'.jpg');
imagedestroy($ims);
imagedestroy($imd);
echo 'Thumbnail Created and Saved at the Destination as '. $thumb .'.jpg';
}
但是我质疑你所要求的背后的逻辑...因为这表明你创建的每个缩略图都只是一个序列号,都存储在同一个目录中?
使用 counter
数据库字段或文件可能会更幸运,因为对包含数千个且还在不断增长的文件夹执行 file_exists
可能会影响性能。
因此,基于文件的 counter
解决方案可能是:
function createThumb($fpath)
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), imagesy($ims));
$thumb = file_get_contents('saveimages/thumbcount.txt');
$thumb++; file_put_contents('saveimages/thumbcount.txt',$thumb);
imagejpeg($imd,'saveimages/'. $thumb .'.jpg');
imagedestroy($ims);
imagedestroy($imd);
echo 'Thumbnail Created and Saved at the Destination as '. $thumb .'.jpg';
}
一定要用 1
填充 thumbcount.txt
,这样它就有了开头。