php 图像上传者cakephp 3并调整大小
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 图像上传者cakephp 3并调整大小相关的知识,希望对你有一定的参考价值。
//function back-end
public function add()
{
$product = $this->Products->newEntity();
if ($this->request->is('post')) {
$this->loadComponent('Image');
$exploded = explode('.', $this->request->data['image']['name']);
$ext = $exploded[count($exploded) - 1];
$file_tmp = $this->request->data['image']['tmp_name'];
$imageWidth = getimagesize($file_tmp)[0];
$imgHeight = getimagesize($file_tmp)[1];
$largeDir = WWW_ROOT . 'images' . DS . 'large' . DS;
$smallDir = WWW_ROOT . 'images' . DS . 'small' . DS;
$imageName = strtolower(Inflector::slug($this->request->data['name']) . '.' . $ext);
if (!empty($imageName) && is_uploaded_file($file_tmp)) {
$this->request->data['image'] = $imageName;
$imgWidth = ($imageWidth < '500')? $imageWidth : 500;
$imgHeight = ($imageWidth < '500')? $imgHeight : 500;
$this->Image->resample($file_tmp, $largeDir, $imageName, $imgWidth, $imgHeight, false, false);
$this->Image->resample($file_tmp, $smallDir, $imageName, 300, 300, false, false);
}else {
unset($this->request->data['image']);
}
$product = $this->Products->patchEntity($product, $this->request->data);
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
}
$categories = $this->Products->Categories->find('list', ['limit' => 200]);
$this->set(compact('product', 'categories'));
$this->set('_serialize', ['product']);
}
//front form cakephp 3
<div class="row">
<div class="col-sm-5">
<?= $this->Form->create($product,['type' => 'file']) ?>
<?php echo $this->Form->input('category_id', ['label'=>'Categoría','options' => $categories, 'empty' => true, 'class' => 'form-control']); ?>
<br />
<?php echo $this->Form->input('name', ['label'=>'Nombre', 'class' => 'form-control', 'placeholder'=>'Marcadores Carioca ']); ?>
<br />
<?php echo $this->Form->input('slug', ['label'=>'Slug', 'class' => 'form-control', 'placeholder'=>'marcadores-carioca ']); ?>
<br />
<?php echo $this->Form->input('description', ['label'=>'Descripción','class' => 'form-control', 'rows' => 8]); ?>
<br />
<?php echo $this->Form->input('image', ['label'=>'Imagen','type' => 'file', 'label' => 'Imagen', 'class' => 'form-control']); ?>
<br />
<?php echo $this->Form->button('Guardar', ['class' => 'btn btn-primary']); ?>
<?php echo $this->Form->end(); ?>
</div>
</div>
以上是关于php 图像上传者cakephp 3并调整大小的主要内容,如果未能解决你的问题,请参考以下文章