- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有小问题。我需要存储新闻文章中的一些图像,为此,我使用了这篇文章中的小型调整大小类:Resizing the image in php for viewing purposes only。
<?php
Class resize
{
// *** Class variables
private $image;
private $width;
private $height;
private $imageResized;
function __construct($fileName)
{
// *** Open up the file
$this->image = $this->openImage($fileName);
// *** Get width and height
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
}
## --------------------------------------------------------
private function openImage($file)
{
// *** Get extension
$extension = strtolower(strrchr($file, '.'));
switch($extension)
{
case '.jpg':
case '.jpeg':
$img = @imagecreatefromjpeg($file);
break;
case '.gif':
$img = @imagecreatefromgif($file);
break;
case '.png':
$img = @imagecreatefrompng($file);
break;
default:
$img = false;
break;
}
return $img;
}
## --------------------------------------------------------
public function resizeImage($newWidth, $newHeight, $option="auto")
{
// *** Get optimal width and height - based on $option
$optionArray = $this->getDimensions($newWidth, $newHeight, $option);
$optimalWidth = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];
// *** Resample - create image canvas of x, y size
$this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
// *** if option is 'crop', then crop too
if ($option == 'crop') {
$this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
}
}
## --------------------------------------------------------
private function getDimensions($newWidth, $newHeight, $option)
{
switch ($option)
{
case 'exact':
$optimalWidth = $newWidth;
$optimalHeight= $newHeight;
break;
case 'portrait':
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
break;
case 'landscape':
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
break;
case 'auto':
$optionArray = $this->getSizeByAuto($newWidth, $newHeight);
$optimalWidth = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];
break;
case 'crop':
$optionArray = $this->getOptimalCrop($newWidth, $newHeight);
$optimalWidth = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];
break;
}
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
## --------------------------------------------------------
private function getSizeByFixedHeight($newHeight)
{
$ratio = $this->width / $this->height;
$newWidth = $newHeight * $ratio;
return $newWidth;
}
private function getSizeByFixedWidth($newWidth)
{
$ratio = $this->height / $this->width;
$newHeight = $newWidth * $ratio;
return $newHeight;
}
private function getSizeByAuto($newWidth, $newHeight)
{
if ($this->height < $this->width)
// *** Image to be resized is wider (landscape)
{
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
}
elseif ($this->height > $this->width)
// *** Image to be resized is taller (portrait)
{
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
}
else
// *** Image to be resizerd is a square
{
if ($newHeight < $newWidth) {
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
} else if ($newHeight > $newWidth) {
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
} else {
// *** Sqaure being resized to a square
$optimalWidth = $newWidth;
$optimalHeight= $newHeight;
}
}
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
## --------------------------------------------------------
private function getOptimalCrop($newWidth, $newHeight)
{
$heightRatio = $this->height / $newHeight;
$widthRatio = $this->width / $newWidth;
if ($heightRatio < $widthRatio) {
$optimalRatio = $heightRatio;
} else {
$optimalRatio = $widthRatio;
}
$optimalHeight = $this->height / $optimalRatio;
$optimalWidth = $this->width / $optimalRatio;
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
## --------------------------------------------------------
private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
{
// *** Find center - this will be used for the crop
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
$crop = $this->imageResized;
//imagedestroy($this->imageResized);
// *** Now crop from center to exact requested size
$this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
}
## --------------------------------------------------------
public function saveImage($savePath, $imageQuality="100")
{
// *** Get extension
$extension = strrchr($savePath, '.');
$extension = strtolower($extension);
switch($extension)
{
case '.jpg':
case '.jpeg':
if (imagetypes() & IMG_JPG) {
imagejpeg($this->imageResized, $savePath, $imageQuality);
}
break;
case '.gif':
if (imagetypes() & IMG_GIF) {
imagegif($this->imageResized, $savePath);
}
break;
case '.png':
// *** Scale quality from 0-100 to 0-9
$scaleQuality = round(($imageQuality/100) * 9);
// *** Invert quality setting as 0 is best, not 9
$invertScaleQuality = 9 - $scaleQuality;
if (imagetypes() & IMG_PNG) {
imagepng($this->imageResized, $savePath, $invertScaleQuality);
}
break;
// ... etc
default:
// *** No extension - No save.
break;
}
imagedestroy($this->imageResized);
}
## --------------------------------------------------------
}
?>
但这对我不起作用,我也不知道为什么。
for($i = 1; $i <= $imagesCount; $i++){
$img = @imagecreatefromjpeg($_FILES["files"]["tmp_name"][$i-1]);
$imgWidth = imagesx($img);
echo(json_encode($imgWidth));
}
例如,这将输出宽度1903(px)。
for($i = 1; $i <= $imagesCount; $i++){
$uploadImage = new resize($_FILES["files"]["tmp_name"][$i-1]);
//$uploadImage -> resizeImage(1920, 1080, 'landscape');
//$uploadImage -> saveImage("news/" . $newsID["newsID"] . "/full/" . $i . ".jpg", 100);
}
imagesx()抛出错误
"imagesx() expects parameter 1 to be resource, bool given in..."
imagesy()
。
最佳答案
所以我终于做到了:
编辑的类(class):
function __construct($fileName, $extension)
{
// *** Open up the file
$this->image = $this->openImage($fileName, $extension);
echo(json_encode($this->image));
// *** Get width and height
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
}
## --------------------------------------------------------
private function openImage($file, $extension)
{
switch($extension)
{
case '.jpg':
case '.jpeg':
$img = @imagecreatefromjpeg($file);
break;
case '.gif':
$img = @imagecreatefromgif($file);
break;
case '.png':
$img = @imagecreatefrompng($file);
break;
default:
$img = false;
break;
}
return $img;
}
编辑 call :
for($i = 1; $i <= $imagesCount; $i++){
$file = $_FILES["files"]["tmp_name"][$i-1];
$extension = strtolower(strrchr($_FILES["files"]["name"][$i-1], '.'));
$uploadImage = new resize($file, $extension);
}
因此,现在我可以在服务器端使用正确的文件名并在客户端使用正确的扩展名在类中创建新对象。
关于php - GD库中的images()在类里面不起作用,但可以单独使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63780087/
在本地主机上,该库运行良好,当在生产环境中调用该函数时,php 会抛出一个 fatal error 。 我检查了那里的 phpinfo(),它已启用。 我想看看是否可以在 php.ini 上添加任何内
当我在“配置命令”中的 phpinfo() 输出显示时,如何启用(或者我可能需要安装)GD; --without-gd ? 我的 phpinfo() 输出“Core”中也没有任何内容,其中列出了“gd
当我在“配置命令”中的 phpinfo() 输出显示时,如何启用(或者我可能需要安装)GD; --without-gd ? 我的 phpinfo() 输出“Core”中也没有任何内容,其中列出了“gd
我正在尝试使用 GD 函数:imagettfbbox(),但它未定义。自从我在 Ubuntu 21.10 上以来,我已经编译了我自己的带有 argon2 支持的 PHP 版本。 我是这样配置的: ./
我正在尝试使用 GD 函数:imagettfbbox(),但它未定义。自从我在 Ubuntu 21.10 上以来,我已经编译了我自己的带有 argon2 支持的 PHP 版本。 我是这样配置的: ./
我已经通过一些在线资源自学了机器学习,但我有一个关于梯度下降的问题,但我无法弄清楚。 梯度下降的公式由以下逻辑回归给出: Repeat { θj = θj−α/m∑(hθ(x)−y)xj }
我在 centos 7 服务器和 nginx 网络服务器上使用 laravel 网络框架,我安装了 php71w-gd,但当我想上传文件时仍然出现此错误 Intervention \ Image \
我正在尝试安装 moodle,但它显示缺少/应该启用 php-gd 扩展。然而,gd 已经安装并且是最新的,当我命令 php -v 时,我收到以下错误,如下所示。 PHP Warning: PHP
编译最新的2.0.35版本,用默认的 ./configure编译 当make的时候,出现以下错误 configure.ac:64: error: possibly undefined macro
我有一个函数可以用来接受用户上传的图像,按比例将其缩放到最大宽度/高度为 4,000 像素,还可以生成 400 像素和 800 像素的缩略图。它需要能够处理透明的 PNG 并应用白色背景。 我当前的代
我试图弄清楚为什么我无法在我的 Debian 7 服务器上安装 GD perl 模块。这是我安装核心内容的方式: sudo apt-get install libgd-gd2-perl 然后运行
如何使用 PHP GD 库制作清晰度滤镜? 所以这... 转向这个... 如果用 PHP 做不到,还有什么其他语言可以做到? 最佳答案 我认为他想使用 PHP 的 GD 库。这很简单:function
我尝试使用 php gd 创建验证码图像: $im = imagecreatetruecolor(100, 25); $white = imagecolorallocate($im, 255, 255
我在网上找到了一些关于 PHP+GD 图像处理的内容,但似乎没有一个能给我我想要的东西。 我让人上传任意尺寸的图像,我编写的脚本将图像大小调整为不超过 200 像素宽 x 200 像素高,同时保持宽高
你好, 我试图围绕中心旋转一个圆形图像,然后切掉边。我看到了 imagerotate 函数,但它似乎没有围绕中心旋转。 有人有什么建议吗? 谢谢。 更新:由于它是一个圆,我想剪掉边缘并使我的圆保持相同
我正在尝试在特定图像上添加文本。它工作完美,但图像质量较低,尤其是 papyrus.ttf 字体。如何提高图像中文本的质量。但我需要高质量来打印输出。这是我的代码..它非常简单。 header("Co
我正在使用 jQuery 的 imgAreaSelect插件,以便裁剪图像并保存缩略图以在比例发生变化等情况下使用。不幸的是,结果与我的预期相去甚远,而且我无法做到正确。图像整体调整大小,而不是被裁剪
好吧,一个小背景故事。我正在重新设计我的一个旧网站,我想使用 Myriad Pro对于页面上的标题。这对于 PHP 等将是动态的。 起初我认为我应该只使用 CSS 样式并将 Myriad 定义为主要字
我想使用 GD 裁剪图像,但我希望它围绕特定形状裁剪,而不仅仅是一个正方形。 这可能吗? 最佳答案 一种方法是创建一个所需正方形大小的蒙版,其中包含一个透明的 alpha channel 。然后将其放
在 PHP GD 中,如何在不丢失任何的情况下将真彩色图像转换为调色板 颜色。用imagetruecolortopallete它不起作用。我有一个函数可以遍历所有颜色并过滤它们(例如灰度)。而且它不会
我是一名优秀的程序员,十分优秀!