PHP裁剪图像可固定宽度和高度而不会丢失尺寸比例 - php

我正在寻找创建尺寸为100px x 100px的缩略图。我看过很多文章解释了这些方法,但是如果要保持尺寸比例,大多数结果将是width!= height。

例如,我有一个450px x 350px的图像。我想裁剪到100px x 100px。如果我要保持比例,我最终将得到100px x 77px。当我在行和列中列出这些图像时,这很难看。但是,没有尺寸比例的图像也会看起来很糟糕。

我看过flickr的图片,它们看起来很棒。例如:
缩略图:http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
中等大小:http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
大尺寸:http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg

ks

参考方案

通过仅使用图像的一部分作为缩略图,该缩略图的纵横比为1:1(主要是图像的中心)即可完成。如果仔细看,您可以在flickr缩略图中看到它。

因为您的问题中有“庄稼”,所以我不确定您是否还不知道,但是您想知道什么?

要使用裁剪,下面是一个示例:

//Your Image
$imgSrc = "image.jpg";

//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);

//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);

// calculating the part of the image to use for thumbnail
if ($width > $height) {
  $y = 0;
  $x = ($width - $height) / 2;
  $smallestSide = $height;
} else {
  $x = 0;
  $y = ($height - $width) / 2;
  $smallestSide = $width;
}

// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

PHP mysqli获取查询返回的第一行的值 - php

我正在使用mysqli从数据库中获取某些数据。我正在使用的查询已设置为仅从数据库返回一行。有没有一种方法可以在不使用while循环的情况下获取该行的值?我知道一个while循环对于返回多于一行的行很有用,但是如果不需要while循环,我想避免这种情况,因为不必要的代码是不好的编程。 参考方案 是的-您可以使用:$row = $result->fetch…

php Singleton类实例将在多个会话中保留吗? - php

举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…

更改默认的URL PHP - php

如何更改默认网址。例如www.example.com/index.php-> www.example.com现在,我要将其设置为www.example.com/test.php。我应该在php.ini中进行更改吗? 参考方案 假设您正在使用apache,则可以通过DirectoryIndex指令执行此操作。Check out the docs。