如何在调整大小后删除原始图像

时间:2014-11-25 05:17:38

标签: php html phpmyadmin

我有图片上传脚本,当我们上传图片或图片然后功能调整大小和
    从原始图像制作两个图像,一个是205 * 256,另一个是800 * 500.图像     调整大小是正常工作,但问题是,当我上传和调整图像大小,然后     原始图像也移动到目录...我希望只有调整大小的图像在我的     目录....现在的问题是,我如何从目录中删除原始图像...我     不希望目录中的原始图像..原始图像没有移动到目录..提前谢谢!

<?php
if($_POST['btnSubmit'])
{
for($i=0;$i<count($_FILES["fileUpload"]["name"]);$i++)
{
if(trim($_FILES["fileUpload"]["tmp_name"][$i] !=""))
{
$images = $_FILES["fileUpload"]["tmp_name"][$i];
list($width, $height, $type, $attr) = getimagesize($images);
if($width>600 && $height>480)
{
$new_images = "Thumbnails_".$_FILES["fileUpload"]["name"][$i];
move_uploaded_file($_FILES["fileUpload"]["tmp_name"][$i],"$output_dir".$_FILES["fileUpload"]   ["name"][$i]);
$res = resize(800,500,$_FILES["fileUpload"]["name"][$i],$_FILES["fileUpload"]["type"][$i]); 
$width=205;
$height=256;
$size=GetimageSize($res);
$images_orig = ImageCreateFromJPEG($res);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"$output_dir".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Resize Successful.<br>";
$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
$objDB = mysql_select_db("datbase");
$strSQL = "INSERT INTO gallery ";
$strSQL .="(id,filename,Thumbnails) VALUES ('','".$_FILES["fileUpload"]["name"][$i]."','".$new_images."')";
$objQuery = mysql_query($strSQL);


}
else
{
//upload the image with out changing
}

?>



//This is the resize function
<?php
$output_dir = "MyResize/";
function resize($width, $height,$image,$type){
global $output_dir;
$relPath = $output_dir.$image;
list($w, $h) = getimagesize($relPath);
/* calculate new image size with ratio */
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
/* new file name */
$thumb = $output_dir.$width.'x'.$height.'_'.$image;
//echo $thumb; exit;
/* read binary data from image file */
$imgString = file_get_contents($relPath);
//echo $imgString ; exit;
/* create image from string */
$image = imagecreatefromstring($imgString);
//echo $image; exit;
$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
/* Save image */
switch ($type) {
    case 'image/jpeg':
    {
        //echo $tmp; exit;
        imagejpeg($tmp,$thumb,100);
        break;

    }
    case 'image/jpg':
    {
        imagejpeg($tmp,$thumb,100);
        break;

    }
    case 'image/png':
    {
        imagepng($tmp, $thumb, 0);
        break;
    }
    case 'image/gif':
    {
        imagegif($tmp, $thumb);
        break;
    }
    default:
        break;
}
return $thumb;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}
?>

2 个答案:

答案 0 :(得分:2)

要删除图片,请使用unlink('Path to the file')

类似于Unix C unlink()函数。失败时将生成E_WARNING级别错误

答案 1 :(得分:2)

首先裁剪图像并存储该图像,然后使用取消链接删除原始图像

    unlink('Orignal image path');

取消链接将删除文件夹

中的原始图像