示例#1
0
/**
 * resize or crop images using PHP's libGD support
 *
 * @author Andreas Gohr <*****@*****.**>
 * @author Sebastian Wienecke <*****@*****.**>
 */
function media_resize_imageGD($ext, $from, $from_w, $from_h, $to, $to_w, $to_h, $ofs_x = 0, $ofs_y = 0)
{
    global $conf;
    if ($conf['gdlib'] < 1) {
        return false;
    }
    //no GDlib available or wanted
    // check available memory
    if (!is_mem_available($from_w * $from_h * 4 + $to_w * $to_h * 4)) {
        return false;
    }
    // create an image of the given filetype
    if ($ext == 'jpg' || $ext == 'jpeg') {
        if (!function_exists("imagecreatefromjpeg")) {
            return false;
        }
        $image = @imagecreatefromjpeg($from);
    } elseif ($ext == 'png') {
        if (!function_exists("imagecreatefrompng")) {
            return false;
        }
        $image = @imagecreatefrompng($from);
    } elseif ($ext == 'gif') {
        if (!function_exists("imagecreatefromgif")) {
            return false;
        }
        $image = @imagecreatefromgif($from);
    }
    if (!$image) {
        return false;
    }
    if ($conf['gdlib'] > 1 && function_exists("imagecreatetruecolor") && $ext != 'gif') {
        $newimg = @imagecreatetruecolor($to_w, $to_h);
    }
    if (!$newimg) {
        $newimg = @imagecreate($to_w, $to_h);
    }
    if (!$newimg) {
        imagedestroy($image);
        return false;
    }
    //keep png alpha channel if possible
    if ($ext == 'png' && $conf['gdlib'] > 1 && function_exists('imagesavealpha')) {
        imagealphablending($newimg, false);
        imagesavealpha($newimg, true);
    }
    //keep gif transparent color if possible
    if ($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
        if (function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
            $transcolorindex = @imagecolortransparent($image);
            if ($transcolorindex >= 0) {
                //transparent color exists
                $transcolor = @imagecolorsforindex($image, $transcolorindex);
                $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
                @imagefill($newimg, 0, 0, $transcolorindex);
                @imagecolortransparent($newimg, $transcolorindex);
            } else {
                //filling with white
                $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
                @imagefill($newimg, 0, 0, $whitecolorindex);
            }
        } else {
            //filling with white
            $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
            @imagefill($newimg, 0, 0, $whitecolorindex);
        }
    }
    //try resampling first
    if (function_exists("imagecopyresampled")) {
        if (!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) {
            imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
        }
    } else {
        imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
    }
    $okay = false;
    if ($ext == 'jpg' || $ext == 'jpeg') {
        if (!function_exists('imagejpeg')) {
            $okay = false;
        } else {
            $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
        }
    } elseif ($ext == 'png') {
        if (!function_exists('imagepng')) {
            $okay = false;
        } else {
            $okay = imagepng($newimg, $to);
        }
    } elseif ($ext == 'gif') {
        if (!function_exists('imagegif')) {
            $okay = false;
        } else {
            $okay = imagegif($newimg, $to);
        }
    }
    // destroy GD image ressources
    if ($image) {
        imagedestroy($image);
    }
    if ($newimg) {
        imagedestroy($newimg);
    }
    return $okay;
}
示例#2
0
 function onbackup()
 {
     set_time_limit(0);
     $filedir = TIPASK_ROOT . "/data/db_backup/";
     if (!isset($this->post['backupsubmit']) && !isset($this->get[9])) {
         $sqlfilename = date("Ymd", $this->time) . "_" . random(8);
         $tables = $_ENV['db']->showtables();
         forcemkdir($filedir);
         $filename = $_ENV['db']->get_sqlfile_list($filedir);
         include template('dbbackup', 'admin');
     } else {
         $sqldump = '';
         $type = isset($this->post['type']) ? $this->post['type'] : $this->get[2];
         $sqlfilename = isset($this->post['sqlfilename']) ? $this->post['sqlfilename'] : rawurldecode($this->get[3]);
         $sizelimit = isset($this->post['sizelimit']) ? $this->post['sizelimit'] : intval($this->get[4]);
         $tableid = intval($this->get[5]);
         $startfrom = intval($this->get[6]);
         $volume = intval($this->get[7]) + 1;
         $compression = isset($this->post['compression']) ? $this->post['compression'] : intval($this->get[8]);
         $backupfilename = $filedir . $sqlfilename;
         $backupsubmit = 1;
         $tables = array();
         if (substr(trim(ini_get('memory_limit')), 0, -1) < 32 && substr(trim(ini_get('memory_limit')), 0, -1) > 0) {
             @ini_set('memory_limit', '32M');
         }
         if (!is_mem_available($sizelimit * 1024 * 3)) {
             $this->message($sizelimit . 'KB 大于PHP程序可用值,请设置较小分卷大小值', 'index.php?admin_db/backup');
         }
         switch ($type) {
             case "full":
                 $tables = $_ENV['db']->showtables();
                 break;
             case "stand":
                 $tables = array(DB_TABLEPRE . "category", DB_TABLEPRE . "question", DB_TABLEPRE . "answer", DB_TABLEPRE . "user", DB_TABLEPRE . "setting");
                 break;
             case "min":
                 $tables = array(DB_TABLEPRE . "question", DB_TABLEPRE . "answer");
                 break;
             case "custom":
                 if (!(bool) $this->post['tables']) {
                     $tables = $this->cache->read('backup_tables', '0');
                 } else {
                     $tables = $this->post['tables'];
                     $this->cache->write('backup_tables', $tables);
                 }
                 break;
         }
         if ($sizelimit < 512) {
             $this->message('文件大小限制不要小于512K', 'BACK');
         }
         if (count($tables) == 0) {
             $this->message('请先选择数据表!', 'BACK');
         }
         if (!file_exists($filedir)) {
             forcemkdir($filedir);
         }
         if (!iswriteable($filedir)) {
             $this->message('/data/db_backup 文件夹不可写!', 'index.php?admin_db-backup');
         }
         if (in_array(DB_TABLEPRE . "usergroup", $tables)) {
             $num = array_search(DB_TABLEPRE . "usergroup", $tables);
             $tables[$num] = $tables[0];
             $tables[0] = DB_TABLEPRE . "usergroup";
         }
         if (in_array(DB_TABLEPRE . "user", $tables)) {
             $num = array_search(DB_TABLEPRE . "user", $tables);
             if ($tables[0] == DB_TABLEPRE . "usergroup") {
                 $tables[$num] = $tables[1];
                 $tables[1] = DB_TABLEPRE . "user";
             } else {
                 $tables[$num] = $tables[0];
                 $tables[0] = DB_TABLEPRE . "user";
             }
         }
         $complete = TRUE;
         for (; $complete && $tableid < count($tables) && strlen($sqldump) + 500 < $sizelimit * 1000; $tableid++) {
             $result = $_ENV['db']->sqldumptable($tables[$tableid], $complete, $sizelimit, $startfrom, strlen($sqldump));
             $sqldump .= $result['tabledump'];
             $complete = $result['complete'];
             if ($complete) {
                 $startfrom = 0;
             } else {
                 $startfrom = $result['startfrom'];
             }
         }
         $dumpfile = $backupfilename . "_%s" . '.sql';
         !$complete && $tableid--;
         if (trim($sqldump)) {
             $result = $_ENV['db']->write_to_sql($sqldump, $dumpfile, $volume);
             if (!$result) {
                 $this->message('无法写入sql文件,请返回', 'BACK');
             } else {
                 $url = "index.php?admin_db/backup/{$type}/" . rawurlencode($sqlfilename) . "/{$sizelimit}/{$tableid}/{$startfrom}/{$volume}/{$compression}/{$backupsubmit}";
                 $this->message("<image src='css/default/loading.gif'><br />第 " . $volume . ' 个文件已经完成!正在进入下一个备份!' . "<script type=\"text/javascript\">setTimeout(\"window.location.replace('{$url}');\", 2000);</script>", 'BACK');
             }
         } else {
             $volume--;
             if ($compression && is_mem_available($sizelimit * 1024 * 3 * $volume)) {
                 $_ENV['db']->write_to_zip($backupfilename, $dumpfile, $volume);
             }
             $this->cache->remove('backup_tables');
             $this->message('数据备份成功!', 'admin_db/backup');
         }
     }
 }
示例#3
0
文件: fetch.php 项目: krayon/flyspray
/**
 * resize images using PHP's libGD support
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function resize_imageGD($ext, $from, $from_w, $from_h, $to, $to_w, $to_h)
{
    global $conf;
    if ($conf['gdlib'] < 1) {
        return false;
    }
    //no GDlib available or wanted
    // check available memory
    if (!is_mem_available($from_w * $from_h * 4 + $to_w * $to_h * 4)) {
        return false;
    }
    // create an image of the given filetype
    if ($ext == 'jpg' || $ext == 'jpeg') {
        if (!function_exists("imagecreatefromjpeg")) {
            return false;
        }
        $image = @imagecreatefromjpeg($from);
    } elseif ($ext == 'png') {
        if (!function_exists("imagecreatefrompng")) {
            return false;
        }
        $image = @imagecreatefrompng($from);
    } elseif ($ext == 'gif') {
        if (!function_exists("imagecreatefromgif")) {
            return false;
        }
        $image = @imagecreatefromgif($from);
    }
    if (!$image) {
        return false;
    }
    if ($conf['gdlib'] > 1 && function_exists("imagecreatetruecolor")) {
        $newimg = @imagecreatetruecolor($to_w, $to_h);
    }
    if (!$newimg) {
        $newimg = @imagecreate($to_w, $to_h);
    }
    if (!$newimg) {
        imagedestroy($image);
        return false;
    }
    //keep png alpha channel if possible
    if ($ext == 'png' && $conf['gdlib'] > 1 && function_exists('imagesavealpha')) {
        imagealphablending($newimg, false);
        imagesavealpha($newimg, true);
    }
    //try resampling first
    if (function_exists("imagecopyresampled")) {
        if (!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
            imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
        }
    } else {
        imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
    }
    $okay = false;
    if ($ext == 'jpg' || $ext == 'jpeg') {
        if (!function_exists('imagejpeg')) {
            $okay = false;
        } else {
            $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
        }
    } elseif ($ext == 'png') {
        if (!function_exists('imagepng')) {
            $okay = false;
        } else {
            $okay = imagepng($newimg, $to);
        }
    } elseif ($ext == 'gif') {
        if (!function_exists('imagegif')) {
            $okay = false;
        } else {
            $okay = imagegif($newimg, $to);
        }
    }
    // destroy GD image ressources
    if ($image) {
        imagedestroy($image);
    }
    if ($newimg) {
        imagedestroy($newimg);
    }
    return $okay;
}
示例#4
0
文件: media.php 项目: s7mx1/dokuwiki
/**
 * resize or crop images using PHP's libGD support
 *
 * @author Andreas Gohr <*****@*****.**>
 * @author Sebastian Wienecke <*****@*****.**>
 */
function media_resize_imageGD($ext, $from, $from_w, $from_h, $to, $to_w, $to_h, $ofs_x = 0, $ofs_y = 0)
{
    global $conf;
    if ($conf['gdlib'] < 1) {
        return false;
    }
    //no GDlib available or wanted
    // check available memory
    if (!is_mem_available($from_w * $from_h * 4 + $to_w * $to_h * 4)) {
        return false;
    }
    // create an image of the given filetype
    if ($ext == 'jpg' || $ext == 'jpeg') {
        if (!function_exists("imagecreatefromjpeg")) {
            return false;
        }
        $image = @imagecreatefromjpeg($from);
        // rotate the image according to exif orientation tag
        if ($image) {
            $meta = new JpegMeta($from);
            $orientation = $meta->getField("Orientation");
            switch ($orientation) {
                case 3:
                    // rotate 180 degrees clockwise
                    if ($conf['syslog']) {
                        syslog(LOG_WARNING, '[media.php] media_resize_imageGD:orientation: ' . $orientation . ' rotate 180 degrees clockwise');
                    }
                    $image = imagerotate($image, 180, 0);
                    break;
                case 6:
                    // only rotate to vertical if width is larger than height
                    if (intval($from_w) > intval($from_h)) {
                        // rotage image 90 degrees clockwise
                        if ($conf['syslog']) {
                            syslog(LOG_WARNING, '[media.php] media_resize_imageGD:orientation: ' . $orientation . ' rotate 90 degrees clockwise');
                        }
                        $image = imagerotate($image, -90, 0);
                        if ($conf['syslog']) {
                            syslog(LOG_WARNING, '[media.php] media_resize_imageGD: swap width and height as we have just rotated the image vertically');
                        }
                        $from_w_old = $from_w;
                        $to_w_old = $to_w;
                        $from_w = $from_h;
                        $to_w = $to_h;
                        $from_h = $from_w_old;
                        $to_h = $to_w_old;
                    }
                    break;
                case 8:
                    // only rotate to vertical if width is larger than height
                    if (intval($from_w) > intval($from_h)) {
                        if ($conf['syslog']) {
                            syslog(LOG_WARNING, '[media.php] media_resize_imageGD:orientation: ' . $orientation . ' rotate 90 degrees anti-clockwise');
                        }
                        // rotage image 90 degrees anti-clockwise
                        $image = imagerotate($image, 90, 0);
                        if ($conf['syslog']) {
                            syslog(LOG_WARNING, '[media.php] media_resize_imageGD: swap width and height as we have just rotated the image vertically');
                        }
                        $from_w_old = $from_w;
                        $to_w_old = $to_w;
                        $from_w = $from_h;
                        $to_w = $to_h;
                        $from_h = $from_w_old;
                        $to_h = $to_w_old;
                    }
                    break;
            }
        }
    } elseif ($ext == 'png') {
        if (!function_exists("imagecreatefrompng")) {
            return false;
        }
        $image = @imagecreatefrompng($from);
    } elseif ($ext == 'gif') {
        if (!function_exists("imagecreatefromgif")) {
            return false;
        }
        $image = @imagecreatefromgif($from);
    }
    if (!$image) {
        return false;
    }
    if ($conf['gdlib'] > 1 && function_exists("imagecreatetruecolor") && $ext != 'gif') {
        $newimg = @imagecreatetruecolor($to_w, $to_h);
    }
    if (!$newimg) {
        $newimg = @imagecreate($to_w, $to_h);
    }
    if (!$newimg) {
        imagedestroy($image);
        return false;
    }
    //keep png alpha channel if possible
    if ($ext == 'png' && $conf['gdlib'] > 1 && function_exists('imagesavealpha')) {
        imagealphablending($newimg, false);
        imagesavealpha($newimg, true);
    }
    //keep gif transparent color if possible
    if ($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
        if (function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
            $transcolorindex = @imagecolortransparent($image);
            if ($transcolorindex >= 0) {
                //transparent color exists
                $transcolor = @imagecolorsforindex($image, $transcolorindex);
                $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
                @imagefill($newimg, 0, 0, $transcolorindex);
                @imagecolortransparent($newimg, $transcolorindex);
            } else {
                //filling with white
                $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
                @imagefill($newimg, 0, 0, $whitecolorindex);
            }
        } else {
            //filling with white
            $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
            @imagefill($newimg, 0, 0, $whitecolorindex);
        }
    }
    //try resampling first
    if (function_exists("imagecopyresampled")) {
        if (!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) {
            imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
        }
    } else {
        imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
    }
    $okay = false;
    if ($ext == 'jpg' || $ext == 'jpeg') {
        if (!function_exists('imagejpeg')) {
            $okay = false;
        } else {
            $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
        }
    } elseif ($ext == 'png') {
        if (!function_exists('imagepng')) {
            $okay = false;
        } else {
            // add compress png image if png_qulaity is set
            if ($conf['png_quality']) {
                $okay = imagepng($newimg, $to, $conf['png_quality']);
            } else {
                $okay = imagepng($newimg, $to);
            }
        }
    } elseif ($ext == 'gif') {
        if (!function_exists('imagegif')) {
            $okay = false;
        } else {
            $okay = imagegif($newimg, $to);
        }
    }
    // destroy GD image ressources
    if ($image) {
        imagedestroy($image);
    }
    if ($newimg) {
        imagedestroy($newimg);
    }
    return $okay;
}