/** * 保存记录前要处理的操作 * */ protected function _before_save() { //属性没有值返回 if (is_null($this->_postfile)) { return; } if (!$this->_postfile->isValid()) { throw new QDB_ActiveRecord_ValidateFailedException(array('postfile' => 'postfile is invalid.'), $this); } // 添加图片 $dir = rtrim(Q::ini('appini/teapics/upload_dir'), '/\\') . DS; $date = date('Y-m'); $dest_dir = $dir . $date; Helper_Filesys::mkdirs($dest_dir); $md5 = md5(rand() . '-' . microtime(true)); //上传图片 $img_url = $md5 . '.' . $this->_postfile->extname(); $this->_postfile->move($dest_dir . DS . $img_url); $img_url = "{$date}/{$img_url}"; //生成缩略图 $thumb_name = $md5 . '-thumb.' . '.jpg'; $img = Helper_Image::createFromFile($this->_postfile->filePath(), $this->_postfile->extname()); $width = intval(Q::ini('appini/teapics/thumb_pic_width')); $height = intval(Q::ini('appini/teapics/thumb_pic_width')); $img->crop($width, $height); $fileName = $dest_dir . DS . $thumb_name; $img->saveAsJpeg($fileName); $thumb_name = "{$date}/{$thumb_name}"; //是否数据库有图片 if ($this->thumb_filename && $this->thumb_filename != $thumb_name) { //待删除的图片为数据库的图片 $this->_pending_delete_files['thumb'][] = $this->thumb_filename; } $this->thumb_filename = $thumb_name; if ($this->img_url && $this->img_url != $img_url) { $this->_pending_delete_files['img'][] = $this->img_url; } //赋值到img_url $this->img_url = $img_url; }
<?php if (!$currentSample) { $sample_id = $samples[0]['id']; } ?> <script type="text/javascript"> <?php if ($iframe) { ?> centredOverlayBlock(<?php echo $iframe->width; ?> , <?php echo $iframe->height; ?> ); <?php } else { ?> loadImage2('<?php echo Helper_Image::renderSample($sample_id, Helper_Image::SAMPLE, false); ?> ', '.image', '<?php echo $sample_id; ?> '); <?php } ?> </script>
<?php $prefix = Helper_Image::render(WEBROOT_PATH . 'img/startpage/TMPNAME', Helper_Image::STARTPAGE, false); $dataJS = array(); foreach ($sections as $key => $section) { $dataJS[$key]['sectionName'] = Helper_Text::clean($section['title']); $dataJS[$key]['aUrl'] = Helper_Html::link(array('controller' => 'index', 'action' => 'section', 'param1' => $section['title'])); $dataJS[$key]['imgUrl'] = str_replace('TMPNAME', $section['id'] . '.jpg', $prefix); } ?> <div class="startpage" style="display: none"> <a href="<?php echo $dataJS[0]['aUrl']; ?> "> <div class="rotatorCont"> <img src="<?php echo $dataJS[0]['imgUrl']; ?> &w=1024&h=575" /> </div> <span><?php echo $dataJS[0]['sectionName']; ?> </span> </a> </div> <br style="clear: both" />
/** * 在保持图像长宽比的情况下将图像裁减到指定大小 * * crop() 在缩放图像时,可以保持图像的长宽比,从而保证图像不会拉高或压扁。 * * crop() 默认情况下会按照 $width 和 $height 参数计算出最大缩放比例, * 保持裁减后的图像能够最大程度的充满图片。 * * 例如源图的大小是 800 x 600,而指定的 $width 和 $height 是 200 和 100。 * 那么源图会被首先缩小为 200 x 150 尺寸,然后裁减掉多余的 50 像素高度。 * * 用法: * @code php * $image->crop($width, $height); * @endcode * * 如果希望最终生成图片始终包含完整图像内容,那么应该指定 $options 参数。 * 该参数可用值有: * * - fullimage: 是否保持完整图像 * - pos: 缩放时的对齐方式 * - bgcolor: 缩放时多余部分的背景色 * - enlarge: 是否允许放大 * - reduce: 是否允许缩小 * * 其中 $options['pos'] 参数的可用值有: * * - left: 左对齐 * - right: 右对齐 * - center: 中心对齐 * - top: 顶部对齐 * - bottom: 底部对齐 * - top-left, left-top: 左上角对齐 * - top-right, right-top: 右上角对齐 * - bottom-left, left-bottom: 左下角对齐 * - bottom-right, right-bottom: 右下角对齐 * * 如果指定了无效的 $pos 参数,则等同于指定 center。 * * $options 中的每一个选项都可以单独指定,例如在允许裁减的情况下将图像放到新图片的右下角。 * * @code php * $image->crop($width, $height, array('pos' => 'right-bottom')); * @endcode * * @param int $width 新的宽度 * @param int $height 新的高度 * @param array $options 裁减选项 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function crop($width, $height, $options = array()) { if (is_null($this->_handle)) { return $this; } $default_options = array('fullimage' => false, 'pos' => 'center', 'bgcolor' => '0xfff', 'enlarge' => false, 'reduce' => true); $options = array_merge($default_options, $options); // 创建目标图像 $dest = imagecreatetruecolor($width, $height); // 填充背景色 list($r, $g, $b) = Helper_Image::hex2rgb($options['bgcolor'], '0xffffff'); $bgcolor = imagecolorallocate($dest, $r, $g, $b); imagefilledrectangle($dest, 0, 0, $width, $height, $bgcolor); imagecolordeallocate($dest, $bgcolor); // 根据源图计算长宽比 $full_w = imagesx($this->_handle); $full_h = imagesy($this->_handle); $ratio_w = doubleval($width) / doubleval($full_w); $ratio_h = doubleval($height) / doubleval($full_h); if ($options['fullimage']) { // 如果要保持完整图像,则选择最小的比率 $ratio = $ratio_w < $ratio_h ? $ratio_w : $ratio_h; } else { // 否则选择最大的比率 $ratio = $ratio_w > $ratio_h ? $ratio_w : $ratio_h; } if (!$options['enlarge'] && $ratio > 1) { $ratio = 1; } if (!$options['reduce'] && $ratio < 1) { $ratio = 1; } // 计算目标区域的宽高、位置 $dst_w = $full_w * $ratio; $dst_h = $full_h * $ratio; // 根据 pos 属性来决定如何定位 switch (strtolower($options['pos'])) { case 'left': $dst_x = 0; $dst_y = ($height - $dst_h) / 2; break; case 'right': $dst_x = $width - $dst_w; $dst_y = ($height - $dst_h) / 2; break; case 'top': $dst_x = ($width - $dst_w) / 2; $dst_y = 0; break; case 'bottom': $dst_x = ($width - $dst_w) / 2; $dst_y = $height - $dst_h; break; case 'top-left': case 'left-top': $dst_x = $dst_y = 0; break; case 'top-right': case 'right-top': $dst_x = $width - $dst_w; $dst_y = 0; break; case 'bottom-left': case 'left-bottom': $dst_x = 0; $dst_y = $height - $dst_h; break; case 'bottom-right': case 'right-bottom': $dst_x = $width - $dst_w; $dst_y = $height - $dst_h; break; case 'center': default: $dst_x = ($width - $dst_w) / 2; $dst_y = ($height - $dst_h) / 2; } imagecopyresampled($dest, $this->_handle, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $full_w, $full_h); imagedestroy($this->_handle); $this->_handle = $dest; return $this; }
/** * 在保持图像长宽比的情况下将图像裁减到指定大小 * * @param int $width * @param int $height * @param boolean $highQuality * @param array $nocut */ function crop($width, $height, $highQuality = true, $nocut = null) { if (is_null($this->_handle)) { return; } $dest = imagecreatetruecolor($width, $height); $sx = imagesx($this->_handle); $sy = imagesy($this->_handle); $ratio = doubleval($width) / doubleval($sx); if (!is_array($nocut)) { if ($nocut) { $nocut = array('enabled' => true, 'pos' => 'center', 'bgcolor' => '0xffffff'); } else { $nocut = array('enabled' => false); } } else { $nocut['enabled'] = isset($nocut['enabled']) ? $nocut['enabled'] : true; $nocut['pos'] = isset($nocut['pos']) ? $nocut['pos'] : 'center'; $nocut['bgcolor'] = isset($nocut['bgcolor']) ? $nocut['bgcolor'] : '0xffffff'; } if ($nocut['enabled']) { // 求缩放后的最大宽度和高度 if ($sy * $ratio > $height) { $ratio = doubleval($height) / doubleval($sy); } $dx = $sx * $ratio; $dy = $sy * $ratio; // 根据 pos 属性来决定如何定位原始图片 switch (strtolower($nocut['pos'])) { case 'left': $ox = 0; $oy = ($height - $sy * $ratio) / 2; break; case 'right': $ox = $width - $sx * $ratio; $oy = ($height - $sy * $ratio) / 2; break; case 'top': $ox = ($width - $sx * $ratio) / 2; $oy = 0; break; case 'bottom': $ox = ($width - $sx * $ratio) / 2; $oy = $height - $sy * $ratio; break; case 'top-left': $ox = $oy = 0; break; case 'top-right': $ox = $width - $sx * $ratio; $oy = 0; break; case 'bottom-left': $ox = 0; $oy = $height - $sy * $ratio; break; case 'bottom-right': $ox = $width - $sx * $ratio; $oy = $height - $sy * $ratio; break; default: $ox = ($width - $sx * $ratio) / 2; $oy = ($height - $sy * $ratio) / 2; } list($r, $g, $b) = Helper_Image::hex2rgb($nocut['bgcolor'], '0xffffff'); $bgcolor = imagecolorallocate($dest, $r, $g, $b); imagefilledrectangle($dest, 0, 0, $width, $height, $bgcolor); imagecolordeallocate($dest, $bgcolor); $args = array($dest, $this->_handle, $ox, $oy, 0, 0, $dx, $dy, $sx, $sy); } else { // 允许图像溢出 if ($sy * $ratio < $height) { // 当按照比例缩放后的图像高度小于要求的高度时,只有放弃原始图像右边的部分内容 $ratio = doubleval($sy) / doubleval($height); $sx = $width * $ratio; } elseif ($sy * $ratio > $height) { // 当按照比例缩放后的图像高度大于要求的高度时,只有放弃原始图像底部的部分内容 $ratio = doubleval($sx) / doubleval($width); $sy = $height * $ratio; } $args = array($dest, $this->_handle, 0, 0, 0, 0, $width, $height, $sx, $sy); } if ($highQuality) { call_user_func_array('imagecopyresampled', $args); } else { call_user_func_array('imagecopyresized', $args); } imagedestroy($this->_handle); $this->_handle = $dest; }
/** * 执行上传文件 * * @param QDB_ActiveRecord_Abstract $obj */ function _exec_upload(QDB_ActiveRecord_Abstract $obj) { if (empty($this->_settings['upload_config'])) { return; } $uploader = new Helper_Uploader(); $error = array(); foreach ($this->_settings['upload_config'] as $file_id => $config) { //必需 $post_key = isset($config['post_key']) ? $config['post_key'] : $file_id; if (!$uploader->existsFile($post_key)) { if (isset($config['required']) && $config['required']) { if ($obj->id() && !empty($obj->{$file_id})) { $obj->willChanged($file_id); } else { $error[$post_key]['required'] = $config['errmsgs']['required']; } } continue; } $file = $uploader->file($post_key); $filename = $file->filename(); $extname = $file->extname(); // 验证文件类型 if (isset($config['allowed_filetypes']) && $config['allowed_filetypes']) { if (!$file->isValid($config['allowed_filetypes'])) { $error[$post_key]['allowed_filetypes'] = $config['errmsgs']['allowed_filetypes']; continue; } } else { if ($file->isValid('.php')) { $error[$post_key]['allowed_filetypes'] = "PHP 文件是不允许上传的."; continue; } } //验证文件大小 if (isset($config['max_size']) && $config['max_size'] > 0) { if ($file->filesize() > $config['max_size'] * 1024) { $error[$post_key]['max_size'] = $config['errmsgs']['max_size']; continue; } } // 验证图片尺寸 if (isset($config['image_dimension']) && $config['image_dimension']) { list($width, $height, $type, $attr) = getimagesize($file->filepath()); if (!$width || !$height) { continue; } list($dim_width, $dim_height) = explode('*', $config['image_dimension']); if (isset($dim_width) && $dim_width > 0 && $dim_width != $width || isset($dim_height) && $dim_height > 0 && $dim_height != $height) { $error[$post_key]['image_dimension'] = $config['errmsgs']['image_dimension']; continue; } } $dir = rtrim($config['upload_dir'], '/\\') . DS; $date = date('Y-m'); $dest_dir = $dir . $date; Helper_Filesys::mkdirs($dest_dir); $md5 = md5($filename . '-' . microtime(true)); $_prop_filename = ''; //如果上图片 if (isset($config['is_image']) && $config['is_image'] || isset($config['large']) || isset($config['thumb'])) { $pic_filename = $md5 . '.jpg'; $image = Helper_Image::createFromFile($file->filepath(), $file->extname()); // 生成大图 if (isset($config['large'])) { list($w, $h) = explode('*', $config['large']); $image->crop($w, $h); } $image->saveAsJpeg($dest_dir . DS . $pic_filename); $_prop_filename = "{$date}/{$pic_filename}"; // 生成缩略图 if (isset($config['thumb'])) { $thumb_filename = $md5 . '-thumb.jpg'; list($w, $h) = explode('*', $config['thumb']); $image->crop($w, $h); $image->saveAsJpeg($dest_dir . DS . $thumb_filename); } // 销毁图片资源并删除上传文件 $image->destroy(); $file->unlink(); } else { $file_name = $md5 . '.' . $extname; $file->move($dest_dir . DS . $file_name); $_prop_filename = "{$date}/{$file_name}"; } //如果是更新,删除原有的 if ($obj->id() && !empty($obj->{$file_id})) { $this->_delete_upload_file($obj, $file_id); } //向属性负值 $obj->{$file_id} = $_prop_filename; } if (!empty($error)) { throw new QDB_ActiveRecord_ValidateFailedException($error, $obj); } }
?> "> <a href="<?php echo Helper_Html::link(array('controller' => 'index', 'action' => 'project', 'param1' => $sample['project_title'], 'param2' => $sample['sample_id'])); ?> " title="<?php echo htmlspecialchars($sample['project_title']); ?> " class="loadingBg"> <?php if (!Helper_Js::isEnabledJs()) { echo '<noscript>'; echo Helper_Image::renderSample($sample['sample_id'], Helper_Image::PREVIEW_PROJECT, array('alt' => $sample['sample_title'])); echo '</noscript>'; } ?> </a> <span><?php echo $sample['project_title']; ?> </span> </div> <?php } ?> </div> </div>
<tr> <?php foreach ($data[0] as $column => $tmp) { echo "<th>{$column}</th>"; } echo '<th></th>'; ?> </tr> <?php foreach ($data as $row) { echo '<tr ' . $statusClass[$row['id']] . '>'; foreach ($row as $key => $val) { echo "<td>"; if ($key == 'img') { echo Helper_Image::render($val, Helper_Image::ADMINPREVIEW); } elseif ($key == 'title' && isset($prefixUrl)) { echo '<a href="' . Helper_Html::link(array('controller' => 'admin', 'action' => $prefixUrl, 'param1' => $row['id'])) . '" title="' . $prefixUrlTitle . '"> ' . $val . '</a>'; } elseif ($key == 'created' || $key == 'updated') { echo Helper_Time::relativeTime($val); } elseif ($key == 'text') { echo htmlspecialchars($val); } else { echo $val; } echo "</td>"; } echo '<td><a href="' . Helper_Html::link(array('controller' => 'admin', 'action' => 'edit', 'param1' => $prefix, 'param2' => $row['id'])) . '">edit</a>';
<?php } ?> </div> <!-- end block preView sample --> <div class="mainContent"> <div> <?php if (isset($samples[$sapmlesCurrentArrayId]['id'])) { $alt = ''; if ($samples[$sapmlesCurrentArrayId]['title']) { $alt = Helper_Text::clean($samples[$sapmlesCurrentArrayId]['title']); } echo Helper_Image::renderSample($samples[$sapmlesCurrentArrayId]['id'], Helper_Image::SAMPLE, array('alt' => $alt, 'title' => $alt)); } ?> </div> <div class="sampleText"> <?php if (isset($samples[$sapmlesCurrentArrayId]['id']) && $samples[$sapmlesCurrentArrayId]['title']) { echo sprintf($formatTextSample, Helper_Text::clean($samples[$sapmlesCurrentArrayId]['title']), Helper_Text::clean($samples[$sapmlesCurrentArrayId]['text'])); } ?> </div> </div> </div>