【问题标题】:How to trim image with php?如何用php修剪图像?
【发布时间】:2015-09-01 18:54:03
【问题描述】:

这是我的 index.php 文件,我已将图像从 tmp 文件夹上传并移动到图像文件夹,并且工作正常。但是现在我想修剪显示的图像,每张图片都应该显示相同的大小,我尝试了很多方法但它不起作用。 需要帮助!

<?php
require_once('appvars.php');
include 'db_connect.php';

$sql = "SELECT * FROM gitarwar";
$data = mysqli_query($dbc, $sql);

echo "<table>";
while ($row = mysqli_fetch_array($data)) 
{
    echo '<tr><td>';
    echo '<strong>' . $row['Score'] . '</strong><br/>';
    echo '<strong>Name:</strong>' . $row['Name'] . '<br/>';
    echo '<strong>Datetime:</strong>' . $row['Datetime'] . '<br/>';
    echo '<img src="' .GW_UPLOADPATH . $row['Screenshot'] . '"  alt="Score image" />';
    echo "</tr>";
}
echo"</table>";
mysqli_close($dbc);
?>

【问题讨论】:

  • 这是一个css问题,不一定是php。只需将宽度和高度设置为您喜欢的内联或 css 文件
  • 或者修剪图片服务器端:stackoverflow.com/questions/1654449/…
  • 但实际上我想用php而不是css
  • 向我们展示您尝试过的一些方法。它将帮助我们了解您实际想要做什么。

标签: php


【解决方案1】:

我之前也有过类似的任务。我的工作是检查图像,如果它小于我的$newWidth$newHeight,它会添加。

$imageUrl = [PATH OR URL TO YOUR IMAGE FILE];

$imageContent = file_get_contents($imageUrl);
$im = imagecreatefromstring($imageContent);
$width = imagesx($im);
$height = imagesy($im);

$newwidth = 300;
$newheight = 300;

$output = imagecreatetruecolor($newwidth, $newheight);

imagealphablending($output, false);
$transparency = imagecolorallocatealpha($output, 0, 0, 0, 127);
imagefill($output, 0, 0, $transparency);
imagesavealpha($output, true);

imagecopymerge($output, $im, ($width < 300 ? (300 - $width) / 2 : 0), ($height < 300 ? (300 - $height) / 2 : 0), 0, 0, $width, $height, 100);

//Show the image
header('Content-Type: image/png');
imagepng($output); //You can save it with another parameter what is a path
imagedestroy($output);
imagedestroy($im);

你需要改变这一行:

imagecopymerge($output, $im, ($width < 300 ? (300 - $width) / 2 : 0), ($height < 300 ? (300 - $height) / 2 : 0), 0, 0, $width, $height, 100);

适合你的尺寸。

这实际上并不是您想要的,但它可能是一个很好的起点。

【讨论】:

    【解决方案2】:

    上传图片时可以使用imagecreatefrom(jpeg|png|gif)函数进行裁剪。

    $file = "images/" . $_FILES['image']['name'];
    $tmp_file = $_FILES['image']['tmp_name'];
    $type = $_FILES['images']['type'];
    if (move_uploaded_file($tmp_file, $file)) {
            chmod($file, 0777);
            // check the type of image, you can do this for other types too, just change the imagecreatefrom function to the type you want to save the image as
            if ($type === 'image/jpeg') {
                $jpeg = imagecreatefromjpeg($file);
                $jpeg_width = imagesx($jpeg);
                $jpeg_height = imagesy($jpeg);
                $new_width = imagesx($jpeg) / 4;          // Fix the width of the thumb nail images, change the width of image here
                $new_height = imagesy($jpeg) / 4; // change the height of image here
    
                //new image
                $new_image = imagecreatetruecolor($new_width, $new_height);
                imagecopyresampled($new_image, $jpeg, 0, 0, 0, 0, $new_width, $new_height, $jpeg_width, $jpeg_height);
                imagejpeg($new_image, $thumb);
                chmod($thumb, 0777);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 2016-12-13
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      相关资源
      最近更新 更多