ZF cake:
<?php
class FkuController extends Zend_Controller_Action {
var $image;
var $image_type;
public function store_uploaded_image($html_element_name, $new_img_width, $new_img_height) {
$target_dir = APPLICATION_PATH . "/../public/1/";
$target_file = $target_dir . basename($_FILES[$html_element_name]["name"]);
//$image = new SimpleImage();
$this->load($_FILES[$html_element_name]['tmp_name']);
$this->resize($new_img_width, $new_img_height);
$this->save($target_file);
return $target_file;
//return name of saved file in case you want to store it in you database or show confirmation message to user
public function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
public function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
public function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
public function getWidth() {
return imagesx($this->image);
}
public function getHeight() {
return imagesy($this->image);
}
public function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
public function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
public function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
public function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
public function savepicAction() {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$this->_response->setHeader('Access-Control-Allow-Origin', '*');
$this->db = Application_Model_Db::db_load();
$ouser = $_POST['ousername'];
$fdata = 'empty';
if (isset($_FILES['picture']) && $_FILES['picture']['size'] > 0) {
$file_size = $_FILES['picture']['size'];
$tmpName = $_FILES['picture']['tmp_name'];
//Determine filetype
switch ($_FILES['picture']['type']) {
case 'image/jpeg': $ext = "jpg"; break;
case 'image/png': $ext = "png"; break;
case 'image/jpg': $ext = "jpg"; break;
case 'image/bmp': $ext = "bmp"; break;
case 'image/gif': $ext = "gif"; break;
default: $ext = ''; break;
}
if($ext) {
//if($file_size<400000) {
$img = $this->store_uploaded_image('picture', 90,82);
//$fp = fopen($tmpName, 'r');
$fp = fopen($img, 'r');
$fdata = fread($fp, filesize($tmpName));
$fdata = base64_encode($fdata);
fclose($fp);
//}
}
}
if($fdata=='empty'){
}
else {
$this->db->update('users',
array(
'picture' => $fdata,
),
array('username=?' => $ouser ));
}
}