.htaccess 通过URL动态更改图像大小

hl0ma9xz  于 2023-11-22  发布在  其他
关注(0)|答案(2)|浏览(161)

我有一个名为images的文件夹,在那里我有像图像:hero.jpg,hero_medium.jpg,hero_small.jpg.我的问题是,如果这是正确的解决方案,或者也许它会更好地有一个大的图片,并通过URL更改其大小.目前我已经设法在images文件夹中的img.php文件中做这样的事情:

  1. <?php
  2. header('Content-Type: image/jpeg');
  3. $filename = 'hero.jpg';
  4. list($width_orig, $height_orig) = getimagesize($filename);
  5. if(empty($_GET['w'])){
  6. $width = $width_orig;
  7. } else {
  8. $width = $_GET['w'];
  9. }
  10. $height=$width;
  11. $ratio_orig = $width_orig/$height_orig;
  12. if ($width/$height > $ratio_orig) {
  13. $width = $height*$ratio_orig;
  14. } else {
  15. $height = $width/$ratio_orig;
  16. }
  17. $image_p = imagecreatetruecolor($width, $height);
  18. $image = imagecreatefromjpeg($filename);
  19. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  20. imagejpeg($image_p, null, 100);

字符串
我想通过一个友好的URL来更改照片的大小,例如在php中使用htaccess将hero_medium.jpg更改为768px,这样的事情可能吗?
编辑:苹果在其网站上有一个有趣的情况.图像的URL看起来像下面这样:https://www.apple.com/newsroom/images/environments/stores/Apple_Tower-Theatre-now-open-in-downtown-LA-store-interior-wide-shot_062421_big.jpg.medium.jpg,为什么有big.jpg.medium.jpg?我怀疑他们可能是做一些与htaccess文件,因为有那么多的照片将无法读取,所以我认为他们是动态地重新定位它们.你怎么看?
编辑2:我注意到通过php加载和更改图像需要更长的时间,这真的是一个好主意吗?

hts6caw3

hts6caw31#

您想使用URL参数设置图像的宽度和高度。这对于动态浏览图像很有用。要实现这一点,您可以将宽度和高度参数附加到图像URL。下面是如何执行此操作的示例
Original Image URL:https://example.com/image.jpg
使用URL调整图片大小:https://example.com/image.jpg?width=600&height=400
HTML文件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Dynamic Image Resizing</title>
  5. </head>
  6. <body>
  7. <img src="resize.php?width=600&height=400" alt="Resized Image">
  8. </body>
  9. </html>

字符串
PHP文件名与'resize.php'

  1. // Get the image file path (replace with your image's actual path)
  2. $imagePath = 'image.png';
  3. // Get the requested width and height from the URL parameters
  4. $width = isset($_GET['width']) ? intval($_GET['width']) : null;
  5. $height = isset($_GET['height']) ? intval($_GET['height']) : null;
  6. // Check if both width and height are provided and are valid integers
  7. if ($width && $height && is_numeric($width) && is_numeric($height)) {
  8. // Load the original image
  9. $originalImage = imagecreatefrompng($imagePath);
  10. // Create a new image with the specified width and height
  11. $resizedImage = imagecreatetruecolor($width, $height);
  12. // Resize the original image to the new dimensions
  13. imagecopyresampled($resizedImage, $originalImage, 0, 0, 0, 0, $width, $height, imagesx($originalImage), imagesy($originalImage));
  14. // Output the resized image as a JPEG
  15. header('Content-Type: image/jpeg');
  16. imagepng($resizedImage);
  17. // Clean up resources
  18. imagedestroy($originalImage);
  19. imagedestroy($resizedImage);
  20. } else {
  21. // Handle the case where width and height parameters are missing or invalid
  22. echo 'Invalid parameters';
  23. }

展开查看全部
8ulbf1ek

8ulbf1ek2#

如果你只是想返回一个足够尺寸的图像,有几种方法可以实现这一点。
也许你想看看这个:https://css-tricks.com/a-guide-to-the-responsive-images-syntax-in-html/

相关问题