我有一个允许文件上传和一些文本输入的代码。它使用imagacreatefromjepg和imagecopymerge中的上传文件。我想将上传的文件大小调整为255x175的确定大小。我该怎么做?这是有的:
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
{
$now++;
}
// now let's move the file to its final and allocate it with the new filename
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);
$upload = $uploadFilename;
$im = imagecreatefromjpeg("bg.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
imagettftext($im, 24, 0, 50, 280, $black, $font, $title);
imagettftext($im, 10, 0, 320, 362, $black, $font, $namehere);
imagecopymerge($im, $img2, 30, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
$date_created = date("YmdHis");//get date created
$img_name = "-img_entry.jpg"; //the file name of the generated image
$img_newname = $date_created . $img_name; //datecreated+name
$img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save the image
imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality
$newpath = "/home3/site/public_html/mainfolder/image_entry/";//path to another folder
$newdir = $newpath.$img_newname;
copy ($img_dir, $newdir); //copy to new folder
imagedestroy($im);
希望你能帮我解决这个问题。我已经对帖子Resize image before uploading PHP和Php resize width fix进行了搜索。但我不知道如何在我的情况下应用它。感谢您的帮助。
答案 0 :(得分:0)
紧随其后:imagecopymerge($im,...
行
$mini_width = ...;// calculate desirable width
$mini_height = ...;// calculate desirable height
$mini_img = @ImageCreateTrueColor($mini_width, $mini_height);
imagecopyresampled($mini_img, $img2, 0, 0, 0, 0, $mini_width, $mini_height, imagesx($img2), imagesy($img2));
下一行是$date_created = date(...
将imagejpeg($im, $img_dir , 80);
更改为:
imagejpeg($mini_img, $img_dir , 80);
最后将imagedestroy($im);
更改为imagedestroy($mini_img);
实际上,您可以,但不必为上面创建的所有资源图片调用imagedestroy()
。