简体   繁体   English

降低php中图像的大小和质量,并用作CSS背景图像

[英]Lower the size and quality of image in php and use as CSS background-image

I have a shop design I am working on that sets the images of the products as a div background, and when you hover it makes it bigger from the middle, etc. I have all that working great but the images are too big and cause a ton of lag. 我有一个正在设计的商店设计,将产品图像设置为div背景,当您将鼠标悬停时,它会使其从中间变大,等等。我所做的一切都很好,但图像太大,导致吨的滞后。

I have tried a couple of options, like using PHP to make the image smaller and slightly lower quality but I can't figure out how to get it from a jpeg image in php to a format that CSS will use as a background, since I am not just printing it out as an image on a page and need CSS animations and such to work with it. 我尝试了几种选择,例如使用PHP使图像更小且质量略低,但我不知道如何从php中的jpeg图像转换为CSS将用作背景的格式,因为我不只是将其作为图像打印在页面上,还需要CSS动画等才能使用。

background:url(
<?php
if(!$i['Image']){

}else{ 
$im = $i['Image'];
$image = "../Images/ShopIMG" . $im . ".jpeg"; 
}
$source_image = imagecreatefromjpeg($image);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 300;
$dest_imagey = 200;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, 
$dest_imagey, $source_imagex, $source_imagey);
$imageData = imagejpeg($dest_image,NULL,80);
$src = 'data: '.mime_content_type($dest_image).';base64,'.$imageData;
echo($image);
?>) no-repeat center center;
background-size: 115%;

I would like the new php image in a smaller size and quality be used as the background image, so it doesn't take as much memory on the page and slow it down but it prints out a bunch of random seemingly broken characters. 我希望将新的php图像以较小的尺寸和质量用作背景图像,因此它不会占用页面上太多的内存并将其放慢速度,但是会打印出一些随机的看似损坏的字符。

Splitting the image and uploading a lower quality first solved my issue. 拆分图像并上传较低质量的图片首先解决了我的问题。 Thank you Dharman. 谢谢达曼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM