public function doSaveMedia($v) { if ($v == null) { $image = $this->getImagePath(); if ($this->isNew) { $content = file_get_contents(sfConfig::get('sf_root_dir') . '/public_html' . $image); $size = getimagesize(sfConfig::get('sf_root_dir') . '/public_html' . $image); } else { return $this->getObject()->getImagesId(); } } else { $content = file_get_contents($v->getTempName()); $size = getimagesize($v->getTempName()); } $hash = md5($content); $criteria = new Criteria(); $criteria->add(ImagesPeer::CONTENT_HASH, $hash); $obj = ImagesPeer::doSelectOne($criteria); try { if (empty($obj)) { $obj = new Images(); $obj->setNameDownloadedFile($v->getOriginalName())->setNameFileForPage($v->getOriginalName())->setTypeImg($v->getType())->setHeight($size[2])->setWidth($size[1])->setContentHash($hash . '.jpg')->setContent(base64_encode($content))->setCreatedAt(date("Y-m-d H:i"))->setUpdatedAt(date("Y-m-d H:i"))->save(); } } catch (Exception $e) { echo "stdfdfdfdfrt"; die; } $criteria = new Criteria(); $criteria->add(ImagesPeer::CONTENT_HASH, $obj->getContentHash()); $obj = ImagesPeer::doSelectOne($criteria); return $obj->getId(); }
public function executeResize(sfWebRequest $request) { $hash = $request->getParameter('hash'); $width = $request->getParameter('x'); $height = $request->getParameter('y'); $criteria = new Criteria(); $criteria->add(ImagesPeer::CONTENT_HASH, $hash); $obj = ImagesPeer::doSelectOne($criteria); $content = base64_decode($obj->getContent()); $src = sfConfig::get('sf_root_dir') . '/public_html/uploads/' . $hash; file_put_contents($src, $content); $quality = 100; $rgb = 0xffffff; if (!file_exists($src)) { return false; } $size = getimagesize($src); if ($size === false) { return false; } $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1)); $icfunc = "imagecreatefrom" . $format; if (!function_exists($icfunc)) { return false; } $x_ratio = $width / $size[0]; $y_ratio = $height / $size[1]; $ratio = min($x_ratio, $y_ratio); $use_x_ratio = $x_ratio == $ratio; $new_width = $use_x_ratio ? $width : floor($size[0] * $ratio); $new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio); $new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2); $new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2); $isrc = $icfunc($src); $idest = imagecreatetruecolor($width, $height); imagefill($idest, 0, 0, $rgb); imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]); $dir = sfConfig::get('sf_root_dir') . '/public_html/uploads/' . $width . '/' . $height . '/'; imagejpeg($idest, $dir . $hash, $quality); imagedestroy($isrc); imagedestroy($idest); return $this->renderText(file_get_contents($dir . $hash)); }
public function doSaveMedia($v) { //var_dump($v);die; $content = file_get_contents($v['tmp_name']); $hash = md5($content); $size = getimagesize($v['tmp_name']); $criteria = new Criteria(); $criteria->add(ImagesPeer::CONTENT_HASH, $hash); $obj = ImagesPeer::doSelectOne($criteria); try { if (empty($obj)) { $obj = new Images(); $obj->setNameDownloadedFile($v['name'])->setTypeImg($v['type'])->setHeight($size[2])->setWidth($size[1])->setContentHash($hash . '.jpg')->setContent(base64_encode($content))->setCreatedAt(date("Y-m-d H:i"))->setUpdatedAt(date("Y-m-d H:i"))->save(); } } catch (Exception $e) { echo "stdfdfdfdfrt"; die; } $criteria = new Criteria(); $criteria->add(ImagesPeer::CONTENT_HASH, $obj->getContentHash()); $obj = ImagesPeer::doSelectOne($criteria); // var_dump($obj->getId());die; return $obj->getId(); }