/** * save productPIC * return boolen */ public function productSave() { $user_id = \Yii::$app->user->getId(); $baseimgurl = 'date/upload/wechat/product/'; //生成随机文件名 $basefilename = $user_id . '_' . time() . '_' . rand(1000, 9999); $this->s_img_url = $sfilename = $baseimgurl . 's' . $basefilename . '.' . $this->file->extension; $this->o_img_url = $ofilename = $baseimgurl . 'o' . $basefilename . '.' . $this->file->extension; $this->m_img_url = $mfilename = $baseimgurl . 'm' . $basefilename . '.' . $this->file->extension; $this->b_img_url = $bfilename = $baseimgurl . 'b' . $basefilename . '.' . $this->file->extension; $this->file->saveAs($bfilename); $image = \Yii::$app->image->load($bfilename); //生成中图片 $image->resize(900, 600, Image::NONE); $image->save($mfilename); //生成小图片 $image->resize(450, 300, Image::NONE); $image->save($sfilename); //生成微略图 $image->resize(90, 60, Image::NONE); $image->save($ofilename); $newpic = new Pic(); $newpic->setAttributes(['user_id' => $user_id, 'pic_type' => 0, 'pic_s_img' => '/' . $sfilename, 'pic_m_img' => '/' . $mfilename, 'pic_b_img' => '/' . $bfilename]); if ($newpic->save()) { $this->id = \Yii::$app->db->getLastInsertID(); return true; } return FALSE; }
/** * 微信处理图片 */ public static function fitImage($obj, Users $user) { #判断图片是否开启 $pic_save_status = \Yii::$app->cache->get("qys_pic_save_" . $user->user_id); $count = Pic::find()->where("user_id=:user_id", array(":user_id" => $user->user_id))->count("*"); if ($pic_save_status == false) { $content = "没有开启保存图片功能,该图片不会被服务器保存."; } elseif ($count >= 20) { $content = "图片已经超过20张,不再做保存。"; } else { $count = Pic::find()->where("user_id=:user_id", array(":user_id" => $user->user_id))->count("*"); #获得图片地址 $pic = file_get_contents($obj->PicUrl); $picname = time() . "_" . $user->user_id . ($count + 1) . ".gif"; $pic_path = Yii::getPathOfAlias('webroot') . '/date/uplaod/'; $pic_show_path = '/date/uplaod/'; file_put_contents($pic_path . $picname, $pic); if (file_exists($pic_path . $picname)) { #小图保存 $simage = Yii::app()->image->load($pic_path . $picname); $simage->resize(80, 80); $simage->save($pic_path . "s_" . $picname); #中图保存 $mimage = Yii::app()->image->load($pic_path . $picname); $mimage->resize(240, 240); $mimage->save($pic_path . "m_" . $picname); #大图保存 $bimage = Yii::app()->image->load($pic_path . $picname); $bimage->save($pic_path . "b_" . $picname); $userPic = new Pic(); $picArray = array('user_id' => $user->user_id, 'pic_type' => 0, 'pic_s_img' => $pic_show_path . "s_" . $picname, 'pic_m_img' => $pic_show_path . "m_" . $picname, 'pic_b_img' => $pic_show_path . "n_" . $picname); $userPic->setAttributes($picArray); if ($userPic->validate() && $userPic->save()) { $content = "图片已经保存。"; } else { $content = "图片保存失败,请重新发送。"; } } else { $content = "保存失败。"; } unlink($pic_path . $picname); } WechatCheck::_transmitText($obj, $content); }