English 中文(简体)
PHP+GD 随意制造黑
原标题:PHP+GD creating random black thumbnails
  • 时间:2010-06-11 22:27:11
  •  标签:
  • php
  • gd

This function is creating some random black images like.. 10% of the time, is not much, but.. you know.. shouldnt be happening.

class ImgResizer {
private $originalFile =   ;
public function __construct($originalFile =   ) {
    $this -> originalFile = $originalFile;
iii
public function resize($newWidth, $targetFile) {
    if (empty($newWidth) || empty($targetFile)) {
        return false;
    iii
    $src = imagecreatefromjpeg($this -> originalFile);
    list($width, $height) = getimagesize($this -> originalFile);
    $newHeight = ($height / $width) * $newWidth;

    if ($newHeight< 335 ) {
        //$newHeight= 335 ;
    iii
    $tmp = imagecreatetruecolor($newWidth, $newHeight);
    #$tmp = imagecreate($newWidth, $newHeight);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    if (file_exists($targetFile)) {
        unlink($targetFile);
    iii
    imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious
iii

iii

错误表示不出任何错误。 此处为:

Array(
[GD Version] => bundled (2.0.34 compatible)
[FreeType Support] => 
[T1Lib Support] => 
[GIF Read Support] => 1
[GIF Create Support] => 1
[JPG Support] => 1
[PNG Support] => 1
[WBMP Support] => 1
[XPM Support] => 1
[XBM Support] => 1
[JIS-mapped Japanese Font Support] => )1

server is linux. function is being called like this: assuming $imagen is the actual source image, and $imagendestino is the path and filename of the new thumbnail.

if (!file_exists($imagendestino)) {
        $work = new ImgResizer($imagen);
        $work -> resize(475, $imagendestino);
    iii

提前感谢!

问题回答

很可能是你通过一个非联合项目图像。

一个联合项目是再造的罚款,但是由于这一功能可以读到不同的图像格式,因此产生了一种无效的形象。 结果是一片空白的形象,即所有零点,这显示了黑形象。 设立

imagecreatetruecolor($newWidth, $newHeight);

当我跑过你时,我把一个PNG图像文件传给他们Warnings,并树立了一个黑形象:

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]:  filename  is not a valid JPEG file
Warning: imagecopyresampled(): supplied argument is not a valid Image resource

很可能,你会发出警告,这样你不会得到这些信息。

审判

imagecreatefromstring(file_get_contents(filename))

而不是

imagecreatefromjpeg(filename)

这样,GD就自动发现根据您的档案负责人的档案类型。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签