Php 快速调整图片大小工具/脚本

Php 快速调整图片大小工具/脚本,php,image,image-processing,image-resizing,Php,Image,Image Processing,Image Resizing,我在服务器上的一个文件夹中按日期组织了很多图像(500000+)。我制作了一个PHP脚本来复制和裁剪子文件夹(thumb)中的每个JPG文件,但是速度非常慢,因为PHP不支持多线程 我想知道如何进行。Python是一个很好的选择吗?有没有好的工具,或者如何改进我的调整大小功能 您还可以查看我的,但php cgi支持多线程。为什么不使用exec()?或者,您可以使用shell脚本,对于php cgi的转换?您可以在php中毫无问题地进行转换,模拟线程而不是直接使用线程。实际上,PHP没有本机线程(

我在服务器上的一个文件夹中按日期组织了很多图像(500000+)。我制作了一个PHP脚本来复制和裁剪子文件夹(thumb)中的每个JPG文件,但是速度非常慢,因为PHP不支持多线程

我想知道如何进行。Python是一个很好的选择吗?有没有好的工具,或者如何改进我的调整大小功能


您还可以查看我的

,但php cgi支持多线程。为什么不使用exec()?或者,您可以使用shell脚本,对于php cgi的转换?

您可以在php中毫无问题地进行转换,模拟线程而不是直接使用线程。实际上,PHP没有本机线程(您最终可以使用库,但在您的情况下这不是很有用)

在代码中,而不是调用:

static::Crop($file,$destination,$tn_w = 300,$tn_h =200,$quality = 100,$wmsource = false);
为什么不做:

$array = array($file, $destination, $tn_w = 300, $tn_h = 200, $quality = 100, $wmsource = 0);
$command = "/usr/bin/php crop.php";
foreach ($array as $arg)
{
  $command .= ' ' . escapeshellarg($arg);
}
exec("$command &"); // note the & which release your execution
usleep(100000);
然后将裁剪函数放入
crop.php
,然后像这样调用它:

list($exec, $file, $destination, $tn_w, $tn_h, $quality, $wmsource) = $argv;
static::Crop($file,$destination,$tn_w = 300,$tn_h =200,$quality = 100,$wmsource = false);
这就行了

如果您想避免usleep并控制一次运行的作物数量,也可以使用文件模拟互斥体,这完全取决于您。通过使用此类,您肯定可以在PHP中完成此类工作。

<?php
class thumbnail_images {

// get
var $PathImgOld;
var $PathImgNew;
var $NewWidth;
var $NewHeight;

// tmp
var $mime;

function imagejpeg_new ($NewImg,$path_img) {
    if ($this->mime == 'image/jpeg' or $this->mime == 'image/pjpeg') imagejpeg($NewImg,$path_img);
    elseif ($this->mime == 'image/gif') imagegif($NewImg, $path_img);
    elseif ($this->mime == 'image/png') imagepng($NewImg, $path_img);
    else return(false);
    return(true);
}

function imagecreatefromjpeg_new($path_img) {
    if ($this->mime == 'image/jpeg' or $this->mime == 'image/pjpeg') $OldImg = imagecreatefromjpeg($path_img);
    elseif ($this->mime == 'image/gif') $OldImg = imagecreatefromgif($path_img);
    elseif ($this->mime == 'image/png') $OldImg = imagecreatefrompng($path_img);
    else return(false);
    return($OldImg);
}

function create_thumbnail_images() {
    $PathImgOld = $this->PathImgOld;
    $PathImgNew = $this->PathImgNew;
    $NewWidth = $this->NewWidth;
    $NewHeight = $this->NewHeight;

    $Oldsize = @getimagesize($PathImgOld);
    $this->mime = $Oldsize['mime'];
    $OldWidth = $Oldsize[0];
    $OldHeight = $Oldsize[1];

    if ($NewHeight == '' and $NewWidth != '') {
        $NewHeight = ceil(($OldHeight * $NewWidth) / $OldWidth);
    }
    elseif ($NewWidth == '' and $NewHeight != '') {
        $NewWidth = ceil(($OldWidth * $NewHeight) / $OldHeight);
    }
    elseif ($NewHeight == '' and $NewWidth == '') {
        return(false);
    }

    $OldHeight_castr = ceil(($OldWidth * $NewHeight) / $NewWidth);
    $castr_bottom = ($OldHeight - $OldHeight_castr) / 2;

    $OldWidth_castr = ceil(($OldHeight * $NewWidth) / $NewHeight);
    $castr_right = ($OldWidth - $OldWidth_castr) / 2;

    if ($castr_bottom>0) {
        $OldWidth_castr = $OldWidth;
        $castr_right = 0;
    }
    elseif ($castr_right>0) {
        $OldHeight_castr = $OldHeight;
        $castr_bottom = 0;
    }
    else {
        $OldWidth_castr = $OldWidth;
        $OldHeight_castr = $OldHeight;
        $castr_right = 0;
        $castr_bottom = 0;
    }

    $OldImg = $this->imagecreatefromjpeg_new($PathImgOld);
    if ($OldImg) {
        $NewImg_castr = imagecreatetruecolor($OldWidth_castr, $OldHeight_castr);
        if ($NewImg_castr) {
            imagecopyresampled($NewImg_castr, $OldImg, 0, 0, $castr_right, $castr_bottom, $OldWidth_castr, $OldHeight_castr, $OldWidth_castr, $OldHeight_castr);
            $NewImg = imagecreatetruecolor($NewWidth, $NewHeight);
            if ($NewImg) {
                imagecopyresampled($NewImg, $NewImg_castr, 0, 0, 0, 0, $NewWidth, $NewHeight, $OldWidth_castr, $OldHeight_castr);
                imagedestroy($NewImg_castr);
                imagedestroy($OldImg);
                if (!$this->imagejpeg_new($NewImg, $PathImgNew)) return (false);
                imagedestroy($NewImg);
            }
        }
    }
    else {
        return(false);
    }

    return(true);
}

}
?>

多线程在这方面有帮助吗?除非你一次要处理多张图片,那就是。即使你要使用另一种语言,你也必须利用多线程功能,它不仅仅是自动开始使用你所有的CPU。下载它们、处理它们并上传它们?@Chris,我正在这样做。。。但是仍然很慢。O@thatidiotguy,假设我的电脑上有这些文件只是为了记录:你也可以用它在服务器上调整大小。这只是一个算法,我将很快上传完整的脚本以调整图像大小。我在我的网站中使用了这个,现在添加的是完整的脚本,而不是您可以执行的for循环
$command.=''”。内爆(''''''',$array)。'''美国LEEP的原因是什么(100000)?以防止。OP有500k张图片要裁剪,如果没有睡眠,您可以同时运行500k个Exec。
$width = $_REQUEST['img_width'];
$height = $_REQUEST['img_height'];
// example
$obj_img = new thumbnail_images();
$obj_img->PathImgOld = 'Old_image.jpg'; // Image for resize
$obj_img->PathImgNew = 'my_image_new_formSubURLkki.jpg'; // New Image Path
$obj_img->NewWidth = $width;
$obj_img->NewHeight = $height;
if (!$obj_img->create_thumbnail_images()) echo "error"; 
else {
    echo 'Image Maked andsave in directory';
}