Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php图像在输出中调整大小_Php_Html_Image - Fatal编程技术网

Php图像在输出中调整大小

Php图像在输出中调整大小,php,html,image,Php,Html,Image,我有这个php函数来根据我需要的位置和我希望显示的大小调整图像的大小。 但我的问题是,如何将其设置为在html图像元素中显示调整大小的图像,而不将php内容类型设置为图像? 如果我将content type设置为image,它将在整个页面中显示图像,如果我删除它,它将输出长的未知字符,我如何做到这一点以获得我想要的 <?php function CroppedThumbnail($imagename, $imgwidth, $imgheight){ // Set a max

我有这个php函数来根据我需要的位置和我希望显示的大小调整图像的大小。 但我的问题是,如何将其设置为在html图像元素中显示调整大小的图像,而不将php内容类型设置为图像? 如果我将content type设置为image,它将在整个页面中显示图像,如果我删除它,它将输出长的未知字符,我如何做到这一点以获得我想要的

    <?php
    function CroppedThumbnail($imagename, $imgwidth, $imgheight){
// Set a maximum height and width
$width = $imgwidth;
$height = $imgheight;
// Content type
//header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($imagename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($imagename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, $imagename, 100);
return $imagename;
}

$filename1 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Large.jpg';
$filename2 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Small.jpg';
echo '<img data="image1" src="'.CroppedThumbnail($filename1, 100, 100).'"/>';
echo '<img data="image2" src="'.CroppedThumbnail($filename2, 100, 100).'"/>';
    ?>

我想调整我调用此函数的任何图像的大小

$newFile = 'images/newFile.jpg';

imagejpeg($image_p, $newFile, 100);

return '/' . $newFile;
我已编辑了您的代码:

<?php
function CroppedThumbnail($imagename, $imgwidth, $imgheight){
// Set a maximum height and width
    $width = $imgwidth;
    $height = $imgheight;
// Content type
//header('Content-Type: image/jpeg');
// Get new dimensions
    list($width_orig, $height_orig) = getimagesize($imagename);
    $ratio_orig = $width_orig/$height_orig;
    if ($width/$height > $ratio_orig) {
        $width = $height*$ratio_orig;
    } else {
        $height = $width/$ratio_orig;
    }
// Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($imagename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
    $newFileName = 'images/newFile.jpg';
    imagejpeg($image_p, $newFileName, 100);

    return '/' . $newFileName;
}
$filename1 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Large.jpg';
echo '<img data="image1" src="'.CroppedThumbnail($filename1, 100, 100).'"/>';
?>

我已编辑了您的代码:

<?php
function CroppedThumbnail($imagename, $imgwidth, $imgheight){
// Set a maximum height and width
    $width = $imgwidth;
    $height = $imgheight;
// Content type
//header('Content-Type: image/jpeg');
// Get new dimensions
    list($width_orig, $height_orig) = getimagesize($imagename);
    $ratio_orig = $width_orig/$height_orig;
    if ($width/$height > $ratio_orig) {
        $width = $height*$ratio_orig;
    } else {
        $height = $width/$ratio_orig;
    }
// Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($imagename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
    $newFileName = 'images/newFile.jpg';
    imagejpeg($image_p, $newFileName, 100);

    return '/' . $newFileName;
}
$filename1 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Large.jpg';
echo '<img data="image1" src="'.CroppedThumbnail($filename1, 100, 100).'"/>';
?>

我建议对您的代码进行简单更改,以满足您的需求,并且可以重复使用

在这里,我们可以将调整大小功能分离到单独的文件中

resize.php

<?php
 function CroppedThumbnail($imagename, $imgwidth, $imgheight){

 // Set a maximum height and width
    $width = $imgwidth;
    $height = $imgheight;

// Get new dimensions
   list($width_orig, $height_orig) = getimagesize($imagename);
   $ratio_orig = $width_orig/$height_orig;

   if ($width/$height > $ratio_orig) {
     $width = $height*$ratio_orig;
   } else {
     $height = $width/$ratio_orig;
   }

 // Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($imagename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,  $width_orig, $height_orig);

   // Output
   return imagejpeg($image_p, null, 100);
}

  header('Content-Type: image/jpeg');
  echo CroppedThumbnail($_GET['imagename'], $_GET['width'], $_GET['height'])
?>

现在我们可以使用上面的调整大小文件,通过向请求中传递图像名称、高度和宽度来渲染调整大小的图像。
example.php

 <?php
    $filename1 = 'path and file name of image 1 here';
    $filename2 = 'path and file name of image 2 here';

    echo '<img data="image1" src="imagetest.php?imagename='. $filename1 .'&height=100&width=100"/>';
    echo '<img data="image2" src="imagetest.php?imagename='. $filename2 .'&height=100&width=100"/>';
 ?>

我建议对您的代码进行简单更改,以满足您的需求,并且可以重复使用

在这里,我们可以将调整大小功能分离到单独的文件中

resize.php

<?php
 function CroppedThumbnail($imagename, $imgwidth, $imgheight){

 // Set a maximum height and width
    $width = $imgwidth;
    $height = $imgheight;

// Get new dimensions
   list($width_orig, $height_orig) = getimagesize($imagename);
   $ratio_orig = $width_orig/$height_orig;

   if ($width/$height > $ratio_orig) {
     $width = $height*$ratio_orig;
   } else {
     $height = $width/$ratio_orig;
   }

 // Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($imagename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,  $width_orig, $height_orig);

   // Output
   return imagejpeg($image_p, null, 100);
}

  header('Content-Type: image/jpeg');
  echo CroppedThumbnail($_GET['imagename'], $_GET['width'], $_GET['height'])
?>

现在我们可以使用上面的调整大小文件,通过向请求中传递图像名称、高度和宽度来渲染调整大小的图像。
example.php

 <?php
    $filename1 = 'path and file name of image 1 here';
    $filename2 = 'path and file name of image 2 here';

    echo '<img data="image1" src="imagetest.php?imagename='. $filename1 .'&height=100&width=100"/>';
    echo '<img data="image2" src="imagetest.php?imagename='. $filename2 .'&height=100&width=100"/>';
 ?>

我找到了一个数学方法来完成这项工作

Github回购-

实例-


我找到了一个数学方法来完成这项工作

Github回购-

实例-




imagejpeg返回布尔值,true或false。您必须使用第二个参数作为文件名并返回文件名和路径。@KiwiJuice我确实理解您要求我返回的文件名路径是您做的还是您没有做的?好的,您需要实际创建的文件在img的源代码中使用。在您的例子中,由于第二个参数为null,您当前获得了整个图像流。@KiwiJuicer我没有
返回imagejpeg($image\u p,$imagename,100)但是我得到这个错误,图像“example.com/test.php”无法显示,因为它包含错误。您能在回答中给我看一个代码示例吗?@StuartBrian使用header content type=image还检查图像内容之前是否应该没有任何输出。出现的错误是因为在图像内容之前有一些输出。imagejpeg返回布尔值,true或false。您必须使用第二个参数作为文件名并返回文件名和路径。@KiwiJuice我确实理解您要求我返回的文件名路径是您做的还是您没有做的?好的,您需要实际创建的文件在img的源代码中使用。在您的例子中,由于第二个参数为null,您当前获得了整个图像流。@KiwiJuicer我没有
返回imagejpeg($image\u p,$imagename,100)但是我得到这个错误,图像“example.com/test.php”无法显示,因为它包含错误。您能在回答中给我看一个代码示例吗?@StuartBrian使用header content type=image还检查图像内容之前是否应该没有任何输出。您得到的错误是,在图像内容之前有一些输出。在中,我这样做了,但输出图像再次变大。在代码和内容类型以及输出图像变大之前,我删除了其他输出。我已再次更新它,返回的是我想要输出的图像名称,而不是重新采样的图像,因为您还没有使用裁剪的图像,所以您返回的是原始图像。看看我的例子!很好,它创建了一个新的图像,是okayin做的,但是输出图像是大的。rry我在代码和内容类型之前删除了其他输出,并且输出图像是大的。我要更新它,返回的是图像名称,我要输出的不是重新采样的图像,因为你还没有使用裁剪的图像,你返回的是原始图像。看看我的例子!很好,它创造了新的形象是好的