谢谢你的帮助。
我写了一个脚本,它从外部 url 下载图片并写入服务器。我使用 mysql 数据库来调用图像目标。为此,我必须遵循以下顺序:
- 从 url 下载图片写入我的文件夹
- 将目标写入我的数据库
- 用 mysql_insert_id( $link ) 取最后一个 id;
- 重命名图片。(我不知道如何使用 rename())
这是我的代码
<?php
$source_pic = $arr['thumbnail_url']; \\ comes from a API
$destination_pic = '/var/www/vhosts/domain.com/domain.com/Uploads/thumbnails/'.uniqid($tag_ponter).'.jpg';
$max_width = 460;
$max_height = 345;
$what = getimagesize($source_pic);
switch(strtolower($what['mime']))
{
case 'image/png':
$src = imagecreatefrompng($source_pic);
break;
case 'image/jpeg':
$src = imagecreatefromjpeg($source_pic);
break;
case 'image/gif':
$src = imagecreatefromgif($source_pic);
break;
default: die();
}
list($width,$height)=getimagesize($source_pic);
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) ){
$tn_width = $width;
$tn_height = $height;
}elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}else{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$tmp=imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
imagejpeg($tmp,$destination_pic,100);
imagedestroy($src);
imagedestroy($tmp);
$thumbnail = '/Uploads/thumbnails/'.$tag_pointer.'.jpg';
//Write into Database
$values[] = "(NULL,'" . $GroupID . "','" . $UserID . "','" . $Title . "', '". $Description ."', '" . $tag_pointer . "','" . $arr['url'] . "', '". $arr['provider_name'] ."', '".$Text."', '". $arr['type'] ."', '" . $thumbnail . "', '" . $arr['html'] . "', NOW(), '0', '" . getRealIP() . "','1', '0','0')";
$insert = mysql_query("INSERT INTO contents( ContentID, CatID, UserID, Title, Description, Tag, Link, Provider, Text, Type, Thumbnail, Html, WriteTime, Comments, WriterIP, Active, TotalVotes, VoteSum )
VALUES ".implode(',', $values)."");
$ContentID = mysql_insert_id( $link );
?>
如何重命名文件?这是正确的做法吗?