public function generalAction() { $model = new SettingsModel(); if (isPost()) { $data['realname'] = post('realname'); $data['country'] = post('country'); $data['city'] = post('city'); $data['sex'] = post('sex', 'int'); $data['mm'] = post('mm', 'int'); $data['dd'] = post('dd', 'int'); $data['yyyy'] = post('yyyy', 'int'); $data['about'] = post('about'); $tradelink = urldecode(post('tradelink')); if (preg_match('/' . preg_quote('/?partner=', '/') . '(.*)' . preg_quote('&token=', '/') . '/Us', $tradelink, $match)) { $partner = $match[1]; } if (preg_match('/' . preg_quote('&token=', '/') . '(.*)' . preg_quote('<<<eof', '/') . '/Us', $tradelink . '<<<eof', $match)) { $token = $match[1]; } if ($partner && $token) { $data['partner'] = $partner; $data['token'] = $token; } $model->setSettings(Request::getParam('user')->id, $data); $path = 'public/users/' . Request::getParam('user')->id . '/'; remkdir($path); File::LoadImage($_FILES['file'], $path, 'avatar', 'jpg', null, 0, 2, 184, 184); File::LoadImage($_FILES['file'], $path, 'avatar_m', 'jpg', null, 0, 2, 64, 64); File::LoadImage($_FILES['file'], $path, 'avatar_s', 'jpg', null, 0, 2, 32, 32); redirect(url('settings', 'general')); } $this->view->countrysList = $model->getCountryList(); $this->view->title = Lang::translate('GENERAL_TITLE'); }
/** * used for Category,Brand,Series,(They have something in common) * @param $method | callback function name * @param $table | table in db * @param $base_path | base path for image file in db * @param $pk | primary key in current website's table * @param $dh_pk | primary key in datahouse's table, it will do matching with $pk * @param $image_fields | which fields need update * @param $name_field | when create new image file, this will be used as a name prefix * @param $photo | the array data from dh * @param &$tmp_update | record flag * @param $update_mapping | array('CURRENT_FIELD'=>'DH_FIELD')if need some base field for updating */ public function sync_common($method, $table, $base_path, $pk, $dh_pk = 'sourceid', $image_fields = array(), $name_field, &$photo, &$tmp_update, $update_mapping = NULL) { //---get this (MATCHED PK=sourceid) $sql = "SELECT *\n FROM [|PREFIX|]{$table}\n WHERE {$pk}='" . $this->db->Quote($photo[$dh_pk]) . "'"; $result = $this->db->Query($sql); if ($row = $this->db->Fetch($result)) { //---[DATA FIELDS]update the base information according to the field mapping if ($update_mapping) { $set_base = array(); foreach ($update_mapping as $cur_field => $dh_field) { if (is_array($dh_field)) { //if array,it shows that be dh fields $field_values = array(); foreach ($dh_field as $key) { $field_values[] = $photo[$key]; } $set_base[$cur_field] = implode(',', $field_values); } else { //not array, it's a real value $set_base[$cur_field] = $dh_field; } } if ($this->db->UpdateQuery($table, $set_base, "{$pk}='" . $photo[$dh_pk] . "'") !== FALSE) { $tmp_update['data'] = '1'; } else { $tmp_update['data'] = '0'; } } //---[IMAGE FIELDS]image field that to be update $last_size = ''; //last size that has been update successfully $last_imagefile = ''; //last imagefile that has been saved successfully foreach ($image_fields as $field => $image_size) { $source_file = $this->dh_sourcefile($image_size); //image field $imagefile = NULL; //---default sync status $tmp_update[$image_size] = '0'; //failed //---imagefile exist? $dest_file = $base_path . $row[$field]; if (is_file($dest_file)) { //exist,overwrite him if ($last_size == $image_size) { //it seems that image have transed before,just copy it $source_file = $base_path . $last_imagefile; } //--write new extension $imagefile = copy_fileext($source_file, $row[$field]); //--change dest to use a new extension $dest_file = $base_path . $imagefile; //--transfer if (trans_file($source_file, $dest_file)) { //---if not a same file, delete the old one if ($imagefile != $row[$field]) { unlink($base_path . $row[$field]); } } else { //---mark to not update into database $imagefile = NULL; } } else { //not exist,create a new //--build new image path if ($last_size == $image_size) { //its no need to do transfer,just update the field only $imagefile = $last_imagefile; } else { //a new transfer must start $imagefile = $this->{$method}($row[$name_field], basename($this->current_dhpath), $image_size); $dest_file = $base_path . $imagefile; remkdir(dirname($dest_file)); if (!trans_file($source_file, $dest_file)) { //---mark to not update into database $imagefile = NULL; } } } //---update new imagefile to database if ($imagefile) { $set[$field] = $imagefile; if ($this->db->UpdateQuery($table, $set, "{$pk}='" . $photo[$dh_pk] . "'") !== FALSE) { //---set sync status $tmp_update[$image_size] = '1'; //success //---record last successed item $last_size = $image_size; $last_imagefile = $imagefile; } } } } }
/** * Mkdir recursively * @author Wilson Zeng */ function remkdir($path, $mode = 0777) { if (!file_exists($path)) { remkdir(dirname($path), $mode); mkdir($path, $mode); } }
/** * Function LoadImage * @param array $file ex. $_FILES['name'] * @param string $path ex. 'app/public/' * @param null $name ex. 'name' * @param string $format ex. 'jpg' * @param array $allowedFormats ex. array('jpg' => true, 'gif' => false) * @param int $size - max file size * @param int $resize ex. 0 - no resize(сжать), 1 - обрезать не изменяя размеров, 2 - обрезать симетрически уменьшив * @param int $minHeight * @param int $minWidth * @param int $maxHeight * @param int $maxWidth * @return mixed */ public static function LoadImage($file, $path, $name = null, $format = 'jpg', $allowedFormats = array(), $size = 0, $resize = 0, $minHeight = 0, $minWidth = 0, $maxHeight = 0, $maxWidth = 0) { $data = array('error' => 0); $data['format'] = mb_strtolower(mb_substr($file['name'], mb_strrpos($file['name'], '.') + 1)); $data['new_format'] = $format; $data['path'] = _SYSDIR_ . trim($path, '/') . '/'; $data['tmp_name'] = $file['tmp_name']; $data['size'] = $file['size']; $data['type'] = $file['type']; $data['name'] = $file['name']; // Recursive mkdir remkdir($path); if (!$name) { $data['new_name'] = randomHash(); } else { $data['new_name'] = $name; } if (!is_array($allowedFormats) or empty($allowedFormats)) { $allowedFormats = self::$allowedImageFormats; } if ($allowedFormats[$data['format']] !== true) { $data['error'] = 1; $data['error_msg'] = 'Incorrect file format'; return $data; } if (intval($size) > 0 && $data['size'] > $size) { $data['error'] = 2; $data['error_msg'] = 'File size is too large'; return $data; } if ($data['format'] == 'jpg') { $imageCreateFrom = 'ImageCreateFromJpeg'; } else { $imageCreateFrom = 'ImageCreateFrom' . $data['format']; } if ($data['new_format'] == 'jpg') { $imagePrint = 'imageJpeg'; } else { $imagePrint = 'image' . $data['new_format']; } // Create resource image $img = $imageCreateFrom($file['tmp_name']); $data['height'] = imagesy($img); $data['width'] = imagesx($img); // Min resizing if ($minHeight == 0 && $minWidth == 0) { $data['new_height'] = $data['height']; $data['new_width'] = $data['width']; } else { if ($minHeight != 0 && $minWidth == 0) { $data['new_height'] = $minHeight; $hw = round($data['width'] / $data['height'], 6); $data['new_width'] = round($hw * $minHeight, 0); } else { if ($minHeight == 0 && $minWidth != 0) { $data['new_width'] = $minWidth; $hw = round($data['height'] / $data['width'], 6); $data['new_height'] = round($hw * $minWidth, 0); } else { if ($minHeight != 0 && $minWidth != 0) { $data['new_height'] = $minHeight; $data['new_width'] = $minWidth; } } } } // Max resizing if ($maxHeight != 0 && $maxWidth == 0 && $maxHeight < $data['height']) { $data['new_height'] = $maxHeight; $hw = round($data['width'] / $data['height'], 6); $data['new_width'] = round($hw * $maxHeight, 0); } else { if ($maxHeight == 0 && $maxWidth != 0 && $maxWidth < $data['width']) { $data['new_width'] = $maxWidth; $hw = round($data['height'] / $data['width'], 6); $data['new_height'] = round($hw * $maxWidth, 0); } else { if ($maxHeight != 0 && $maxWidth != 0 && ($maxHeight < $data['height'] or $maxWidth < $data['width'])) { if ($data['height'] > $data['width']) { $data['new_height'] = $maxHeight; $hw = round($data['width'] / $data['height'], 6); $data['new_width'] = round($hw * $maxHeight, 0); } elseif ($data['height'] < $data['width']) { $data['new_width'] = $maxWidth; $hw = round($data['height'] / $data['width'], 6); $data['new_height'] = round($hw * $maxWidth, 0); } } } } if ($resize == 1) { $data['height'] = $data['new_height']; $data['width'] = $data['new_width']; } if ($resize == 2) { if ($data['new_width'] > $data['new_height']) { $hw = round($data['new_height'] / $data['new_width'], 6); $data['height'] = round($hw * $data['width'], 0); } elseif ($data['new_width'] < $data['new_height']) { $hw = round($data['new_width'] / $data['new_height'], 6); $data['width'] = round($hw * $data['height'], 0); } else { if ($data['width'] > $data['height']) { $data['width'] = $data['height']; } else { $data['height'] = $data['width']; } } } $screen = imageCreateTrueColor($data['new_width'], $data['new_height']); if ($data['format'] == 'png') { imagealphablending($screen, false); // Disable pairing colors imagesavealpha($screen, true); // Including the preservation of the alpha channel } imageCopyResampled($screen, $img, 0, 0, 0, 0, $data['new_width'], $data['new_height'], $data['width'], $data['height']); $imagePrint($screen, $data['path'] . $data['new_name'] . '.' . $data['new_format']); imageDestroy($img); return $data; }
/** * Common log * @author Wilson Zeng */ function common_log($message, $type = 'DEBUG', $filename = '') { $now = time(); if (empty($filename)) { $filename = $type . '_' . date('Ymd', $now) . '.log'; } $datetime = date('h:i:s Y/m/d', $now); $row_content = "[{$type}][{$datetime}]{$message}\n"; $fullpath = LOG_FOLDER . $filename; remkdir(dirname($fullpath)); file_put_contents(LOG_FOLDER . $filename, $row_content, FILE_APPEND); }