Actions

Source Code of This Project

/lib/model/doctrine/Images.class.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class Images extends BaseImages
{
    public function setUp()
    {
        parent::setUp();
//    $this->actAs('SoftDelete');
//    $this->actAs('Timestampable');
    }

    /**
     * Delete all files of image
     *
     * @param Doctrine_Connection $conn
     * @return <type>
     */
    public function delete(Doctrine_Connection $conn = null)
    {
        // prepare data
        $imagesPath = sfConfig::get('app_images_path');
        $sizes = $this->getImageSizes();
        $id = $this->getId();
        $ext = sfConfig::get('app_image_format');
        $fileName = $id.'.'.$ext;

        // delete all image copies
        foreach ($sizes as $size) {
            $path = $imagesPath.'/'.$size.'/'.$this->getPathTree($id).'/'.$fileName;
            if(is_file($path)){
                unlink($path);
            }
        }

        return parent::delete($conn);
    }

    /**
     * Get HTML image tag in backend list of images
     *
     * @return string HTML tag
     */
    public function getImageBackendList()
    {
        return sprintf('<img src="%s"/>', $this->buildPathImage('120x0'));
    }

    /**
     * Build path for image
     * If image is not found, create it from an original (0x0)
     *
     * @param string $size e.g. 120x90, 900x0, 0x0
     * @param string $pathType type of image path, 'web' or 'fs'
     * @param boool do resizing from original
     * @return string
     */
    function buildPathImage($size, $pathType = 'web', $resize = false)
    {
        /**
         * check and prepare data
         */
        // init name of file
        $id = $this->getId();
        $ext = sfConfig::get('app_image_format');
        $fileName = $id.'.'.$ext;
        // init sizes
        $sizeOrig = '0x0';
        list($width, $height) = explode('x', $size);
        // init paths
        $pathBase = sfConfig::get('app_images_path');
        $pathTree = $this->getPathTree($id);
        $pathOrig = $pathBase.'/'.$sizeOrig.'/'.$pathTree;
        $pathSize = $pathBase.'/'.$size.'/'.$pathTree;

        // for original size
        if ($size == $sizeOrig) {
            if (!file_exists($pathOrig)){
                mkdir ($pathOrig, 0777, true);
            }
            if ('web' == $pathType){
                return '/uploads/images/'.$sizeOrig.'/'.$pathTree.'/'.$fileName;
            }elseif ('fs' == $pathType){
                return $pathOrig.'/'.$fileName;
            }
        }

        // break, if a original image is not found
        if (!file_exists($pathOrig.'/'.$fileName)){
            return '';
        }

        // if a image is not found, create it from an original
        if (!file_exists($pathSize)){
            mkdir ($pathSize, 0777, true);
        }
        if (!file_exists($pathSize.'/'.$fileName) or $resize == true) {
            $img = new sfImage($pathOrig.'/'.$fileName);
            $img->setQuality(100);
            $img->resize($width, $height);
            $img->saveAs($pathSize.'/'.$fileName);
        }

        // return path
        if ('web' == $pathType){
            return '/uploads/images/'.$size.'/'.$pathTree.'/'.$fileName;
        }
        elseif ('fs' == $pathType){
            return $pathSize.'/'.$fileName;
        }
    }

    /**
     * Generate path tree
     * e.g.: /6/0
     *
     * @param int $id
     * @return string
     */
    public function getPathTree($id)
    {
        return ($id % 10).'/'.floor(($id % 100)/10);
    }

    /**
     * Return types of image size
     *
     * @return array
     */
    function getImageSizes()
    {
        $dir = sfConfig::get('app_images_path');
        $sizes = array_diff(scandir($dir), array('.', '..'));

        return $sizes;
    }
}