我需要你的帮助才能在我的网站中混合两个功能。我的目的是上传图片并调整图片大小。 我有一个功能正在为用户图片做好工作,我有另一个用于上传图片(列表或广告图片),但此功能不会创建拇指图片。它只是上传它。我喜欢做的是像专用userpic函数一样上传和调整大小。
希望你能帮助我。 这是关于用户图片的功能(拇指调整大小工作)public function photo($id = "") {
$target_path = realpath(APPPATH . '../images/users');
//echo $target_path;
if (!is_writable(dirname($target_path))) {
$this->session->set_flashdata('flash_message', $this->Common_model->flash_message('error', 'Sorry! Destination folder is not writable.'));
redirect('users/edit', 'refresh');
} else {
if (!is_dir(realpath(APPPATH . '../images/users') . '/' . $id)) {
//echo $this->path.'/'.$id;
mkdir(realpath(APPPATH . '../images/users') . '/' . $id, 0777, true);
}
$target_path = $target_path . '/' . $id . '/userpic.jpg';
if ($_FILES['upload123']['name'] != '') {
move_uploaded_file($_FILES['upload123']['tmp_name'], $target_path);
$thumb1 = realpath(APPPATH . '../images/users') . '/' . $id . '/userpic_thumb.jpg';
GenerateThumbFile($target_path, $thumb1, 107, 78);
$thumb2 = realpath(APPPATH . '../images/users') . '/' . $id . '/userpic_profile.jpg';
GenerateThumbFile($target_path, $thumb2, 209, 209);
$thumb3 = realpath(APPPATH . '../images/users') . '/' . $id . '/userpic_micro.jpg';
GenerateThumbFile($target_path, $thumb3, 36, 36);
$this->session->set_flashdata('flash_message', $this->Common_model->flash_message('success', 'Your profile photo updated successfully.'));
redirect('users/edit', 'refresh');
} else {
$this->session->set_flashdata('flash_message', $this->Common_model->flash_message('error', 'Please browse your profile photo.'));
redirect('users/edit', 'refresh');
}
}
}
这是我想要实现调整大小的函数:
if ($this->input->post()) {
$listId = $param;
$images = $this->input->post('image');
$is_main = $this->input->post('is_main');
$fimages = $this->Gallery->get_imagesG($listId);
if ($is_main != '') {
foreach ($fimages->result() as $row) {
if ($row->id == $is_main)
$this->Common_model->updateTableData('list_photo', $row->id, NULL, array("is_featured" => 1));
else
$this->Common_model->updateTableData('list_photo', $row->id, NULL, array("is_featured" => 0));
}
}
if (!empty($images)) {
foreach ($images as $key => $value) {
$image_name = $this->Gallery->get_imagesG(NULL, array('id' => $value))->row()->name;
unlink($this->path . '/' . $listId . '/' . $image_name);
$conditions = array("id" => $value);
$this->Common_model->deleteTableData('list_photo', $conditions);
}
}
if (isset($_FILES["userfile"]["name"])) {
$insertData['list_id'] = $listId;
if (!is_dir($this->path . '/' . $listId)) {
//echo $this->path.'/'.$id;
mkdir($this->path . '/' . $listId, 0777, true);
$insertData['is_featured'] = 1;
}
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->path . '/' . $listId,
'encrypt_name' => TRUE,
'remove_spaces' => TRUE
);
//echo $this->path.'/'.$id;
$this->load->library('upload', $config);
$data = $this->upload->do_upload();
if ($data) {
$this->outputData['file'] = $this->upload->data();
$insertData['name'] = $this->outputData['file']['file_name'];
$insertData['created'] = local_to_gmt();
if ($this->outputData['file']['file_name'] != '')
$this->Common_model->insertData('list_photo', $insertData);
}
}
}
答案 0 :(得分:0)
下面的代码中没有任何内容可以生成拇指文件。
你需要这样的东西:
$this->thumb_path = realpath(APPPATH . '../directory/in/codeigniter/site/image/thumb');
然后在$ this-> upload-> data()完成图片上传后,您需要生成拇指(根据您的设置进行调整)
$thumb_config = array(
'source_image' => $data['full_path'],
'new_image' => $this->thumb_path,
'maintain_ratio' => true,
'width' => 200,
'height' => 200,
'quality' => '100%'
);
$this->load->library('image_lib', $thumb_config);
$this->image_lib->resize();
答案 1 :(得分:0)
生成图片拇指,您需要使用Image Processing (ImageMagick)
我提供以下代码来生成图像拇指。
function GenerateThumbnails($sourcePath1 = NULL, $destinationPath1 = NULL, $fileNames = NULL, $width = NULL, $height = NULL) {
try {
$sourcePath = $sourcePath1;
$destinationPath = $sourcePath . $destinationPath1 . "/";
if (isset($_POST['sourcePath'])) {
//$sourcePath = $_SERVER["DOCUMENT_ROOT"] . $_POST['sourcePath'];
$sourcePath = $_POST['sourcePath'];
}
if (isset($_POST['destinationPath'])) {
//$destinationPath = $_SERVER["DOCUMENT_ROOT"] . $_POST['destinationPath'];
$destinationPath = $_POST['destinationPath'];
}
if (isset($_POST['fileNames'])) {
$fileNames = json_decode($_POST['fileNames']);
}
if (isset($_POST['width'])) {
$width = $_POST['width'];
}
if (isset($_POST['height'])) {
$height = $_POST['height'];
}
foreach ($fileNames as $fileName) {
if (is_dir($sourcePath)) {
if (file_exists($sourcePath . $fileName)) {
$fetchFileExtension = array_values(array_filter(explode(".", $fileName)));
$fileExtension = end($fetchFileExtension);
$sourcePathFileName = $sourcePath . $fileName;
$destinationPathFileName = $destinationPath . $fileName;
$image = new Imagick();
$image->readImage($sourcePathFileName);
$image->scaleImage(1000, 0);
$image->setImageColorspace(255);
$image->setImageFormat('jpg');
$image = $image->flattenImages();
$image->thumbnailImage($width, $height, true);
(is_dir($destinationPath)) ? $image->writeImage($destinationPathFileName) : (createDirectory($destinationPath) AND $image->writeImage($destinationPathFileName));
chmod($destinationPathFileName, 0777);
$image->clear();
$image->destroy();
list($width1, $height1) = getimagesize($destinationPathFileName);
$data['orientation'] = ($width1 > $height1) ? 1 : 2;
$data['thumbnailChecksum'] = md5_file($destinationPathFileName);
$data['message'] = "Thumbnail created successfully .";
} else {
$data['message'] = "Thumbnail creation failed.";
}
}
}
return $data;
} catch (Exception $ex) {
print $ex->getMessage();
return false;
}
}