gpt4 book ai didi

php - imagesx() 和 imagesy() 不工作

转载 作者:搜寻专家 更新时间:2023-10-31 21:13:17 24 4
gpt4 key购买 nike

我正在尝试制作缩略图。并且 php 函数有效。我用它制作了缩略图。但是,某些文件不起作用。我已经缩小了问题的范围,但现在卡住了。任何帮助将不胜感激。

图像可以在 http://www.pspsigma.com/image_test.php 找到.左边那个不行。右边的会。两者都是.jpg。两者最初都是通过表单上传的,但已针对堆栈溢出进行了更改,因此该函数无需表单即可运行。

html 是一个简单的脚本,文件输入设置了对这个 .php 文件的操作

PHP:

<?php
$the_name= $_FILES['userfile']['name'];
$tmpname = $_FILES['userfile']['tmp_name'];



$size="1000";
$save_dir="images/thumbs/";
$save_name=$the_name;
$maxisheight= "100";

function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight)
{
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = getimagesize($tmpname);
$type = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}

//check if imorig is set
if (!isset($imorig)) {
echo " imorig is not set, ";
}

//I tried setting $x and $y from the width and height of the original file. Still didn't work.
//although the variables were set.
//this makes me think the problem is with the imagecreatefromjpeg function.
list($width, $height, $type, $attr) = getimagesize($tmpname);
echo "getimagesize width= " . $width . ", ";
echo "getimagesize height= " . $height . ", ";

//possible solution, didn't work
//$x = $width;
//$y = $height;

//This is the problem
$x = imagesx($imorig);
$y = imagesy($imorig);

//check that x and y are set
if (!isset($x) && !isset($y)) {
echo" x and y are not set,";
}
elseif (!isset($x)) {
echo" x is not set";
}
elseif (!isset($y)) {
echo" y is not set";
}
else {
echo "|x= " . $x . " |";
echo "|y= " . $y . " |";
}


$woh = (!$maxisheight)? $gis[0] : $gis[1] ;


if($woh <= $size)
{
$aw = $x;
$ah = $y;
}
else
{
if(!$maxisheight){
$aw = $size;
$ah = $size * $y / $x;
} else {
$aw = $size * $x / $y;
$ah = $size;
}
}
$im = imagecreatetruecolor($aw,$ah);
if (!$im) {
echo " failure: no im variable. ";
}
else {

if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y)) {
if (!imagejpeg($im, $save_dir.$save_name)) {
echo "failure";}
else {
echo "success";}
}
else {
echo" didn't copy resampled";
}
}
}


img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight);
?>

如果我选择 rush vader.jpg 文件并通过此函数将其放入。它回显 x 和 y 已设置,显示它们的值,并成功。

如果我选择 calm.jpg,它将回显“失败:没有 im 变量”。 $x 和 $y 变量将没有值。如果我为 x 和 y 变量分配 getimagesize($tmp_name) 中的值,它会回显“没有复制重新采样”。

这是实际图像的问题吗?还是有什么方法可以更改代码以使每个图像都能正常工作?

谢谢!!!

更新

我回显每个变量以查看是否有问题。问题似乎出在类型上。类型输出为 6,我从 php 手册注释中发现它是 bmp。

我不明白如果扩展名是 .jpg 怎么可能...如果是这样的话。是否可以更改类型以使其成为 .jpg、.png 或 .gif?

最佳答案

图像 calm.jpg 的类型是 6,它是一个 bmp。因此 imagecreatefrom... 将不起作用并且 $imorig 为 NULL。通过从 php 手册中添加一个名为 imagecreatefrombmp 的函数,标签为 6。该函数运行完美。

添加到文件:

function imagecreatefrombmp($p_sFile)  { 
// Load the image into a string
$file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);

$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);

// Process the header
// Structure: http://www.fastgraph.com/help/bmp_header_format.html
if (substr($header,0,4)=="424d")
{
// Cut it in parts of 2 bytes
$header_parts = str_split($header,2);

// Get the width 4 bytes
$width = hexdec($header_parts[19].$header_parts[18]);

// Get the height 4 bytes
$height = hexdec($header_parts[23].$header_parts[22]);

// Get the horz. resolution in pixel per meter, 4 bytes
$dpix = hexdec($header_parts[39]. $header_parts[38]) * 0.0254;

// Get the vert. resolution in pixel per meter, 4 bytes
$dpiy = hexdec($header_parts[43]. $header_parts[42]) * 0.0254;

// Unset the header params
unset($header_parts);
}

// Define starting X and Y
$x = 0;
$y = 1;

// Create newimage
$image = imagecreatetruecolor($width,$height);

// Grab the body from the image
$body = substr($hex,108);

// Calculate if padding at the end-line is needed
// Divided by two to keep overview.
// 1 byte = 2 HEX-chars
$body_size = (strlen($body)/2);
$header_size = ($width*$height);

// Use end-line padding? Only when needed
$usePadding = ($body_size>($header_size*3)+4);

// Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
// Calculate the next DWORD-position in the body
for ($i=0;$i<$body_size;$i+=3)
{
// Calculate line-ending and padding
if ($x>=$width)
{
// If padding needed, ignore image-padding
// Shift i to the ending of the current 32-bit-block
if ($usePadding)
$i += $width%4;

// Reset horizontal position
$x = 0;

// Raise the height-position (bottom-up)
$y++;

// Reached the image-height? Break the for-loop
if ($y>$height)
break;
}

// Calculation of the RGB-pixel (defined as BGR in image-data)
// Define $i_pos as absolute position in the body
$i_pos = $i*2;
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
$b = hexdec($body[$i_pos].$body[$i_pos+1]);

// Calculate and draw the pixel
$color = imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);

// Raise the horizontal position
$x++;
}

// Unset the body / free the memory
unset($body);

// Return image-object
return ($image);
return ($dpix);
return ($dpiy);
}

添加:

case "6": $imorig = imagecreatefrombmp($tmpname); break;
case "15": $imorig = imagecreatefromwbmp($tmpname); break;

之后

case "3": $imorig = imagecreatefrompng($tmpname); break;

经过测试。一切正常。谢谢大家!

关于php - imagesx() 和 imagesy() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com