WebP sanannen tsarin hoto ne na zamani wanda ke ba da babban rashi da matsi don hotuna akan yanar gizo. Google ne ya haɓaka wannan sigar hoton kuma an yi niyya don maye gurbin tsarin fayil JPEG, PNG, da GIF. Yana goyan bayan duka asara da matsi mara asara, haka nan rayarwa da bayyana gaskiya. Suna kuma shahara saboda fayil ɗin WebP gabaɗaya ya fi na JPEG na gargajiya don haka gidajen yanar gizo suna ɗaukar sauri kuma suna ba masu amfani ƙwarewa mafi kyau. Koyaya, yawancin tsarin al’ada har yanzu suna buƙatar tsarin hoto na raster don ajiya da dalilai na sarrafawa saboda haka, muna da buƙatu na yin jujjuyawar babban WebP zuwa JPG. Don haka a cikin wannan labarin, za mu tattauna dalla-dalla kan yadda ake canza WebP zuwa JPG, WebP zuwa PNG da WebP zuwa tsarin GIF.
- WebP zuwa JPG REST API
- Maida WebP zuwa JPEG a Java
- WebP zuwa PNG a Java
- Yanar gizo zuwa GIF ta amfani da Umarnin CURL
WebP zuwa JPG REST API
Maganin tushen mu na REST Aspose.Imaging Cloud SDK don Java ingantaccen sarrafa hoto ne da canza hoto API. Tare da taimakon wannan API, zaku iya aiwatar da damar sarrafa hoto (gyara da canzawa) a cikin aikace-aikacenku. Yana goyan bayan hotunan raster, fayilolin Photoshop, Metafiles da sauran tsari kamar tsarin WebP. Don haka gwargwadon iyawar wannan labarin, za mu yi amfani da wannan API don canza WebP zuwa JPEG. Yanzu don farawa da amfani da SDK, muna buƙatar ƙara bayaninsa a cikin aikin java ɗinmu ta haɗa da bin bayanai a cikin pom.xml (maven build type project).
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>https://artifact.aspose.cloud/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging-cloud</artifactId>
<version>22.4</version>
</dependency>
</dependencies>
Da zarar an ƙara bayanin REST API a cikin aikin Java, da fatan za a sami takaddun shaidar abokin ciniki daga Cloud Dashboard. In ba haka ba, kuna buƙatar fara rajistar asusun kyauta yayin amfani da ingantaccen adireshin imel.
Maida WebP zuwa JPEG a Java
Bari mu tattauna cikakkun bayanai kan yadda ake lodawa da canza WebP zuwa tsarin JPG ta amfani da snippet code na Java.
- Da farko, ƙirƙiri wani abu na ImagingApi ta amfani da keɓaɓɓen takaddun shaida
- Na biyu, loda hoton WebP ta amfani da hanyar readAllBytes(…) sannan ka wuce zuwa tsararrun byte[]
- Abu na uku, ƙirƙiri misali na UploadFileRequest ta samar da Hoton Yanar Gizo a matsayin hujja da loda shi zuwa ma’ajiyar girgije ta amfani da hanyar uploadFile(…)
- Yanzu ƙirƙiri wani abu na ConvertImageRequest wanda ke ɗaukar sunan hoton WebP da tsarin JPG azaman muhawara
- Kira hanyar da za ta canzaImage(…) don musanya WebP zuwa tsarin JPEG. Ana mayar da fitarwa azaman rafin amsawa
- A ƙarshe, ajiye sakamakon JPG zuwa faifan gida ta amfani da abu FileOutputStream
// Samu ClientID da ClientSecret daga https://dashboard.aspose.cloud/
String clientId = "bb959721-5780-4be6-be35-ff5c3a6aa4a2";
String clientSecret = "4d84d5f6584160cbd91dba1fe145db14";
// haifar da Hoto abu
ImagingApi imageApi = new ImagingApi(clientSecret, clientId);
// ɗora hoton WebP daga faifan gida
File file1 = new File("WEBPSampleImage.webp");
byte[] imageStream = Files.readAllBytes(file1.toPath());
// ƙirƙiri abin buƙatar loda fayil
UploadFileRequest uploadRequest = new UploadFileRequest("source.webp",imageStream,null);
// loda hoton WebP zuwa ma'ajiyar gajimare
imageApi.uploadFile(uploadRequest);
// saka tsarin fitarwa azaman JPEG
String format = "jpg";
// Ƙirƙiri abin buƙatar canza hoto
ConvertImageRequest convertImage = new ConvertImageRequest("source.webp", format, null, null);
// canza WebP zuwa JPEG kuma dawo da fitarwa a cikin rafin amsawa
byte[] resultantImage = imageApi.convertImage(convertImage);
// Ajiye sakamakon JPG zuwa faifan gida
FileOutputStream fos = new FileOutputStream("/Users/nayyer/Documents/" + "Converted.jpg");
fos.write(resultantImage);
fos.close();
Hoton Yanar Gizo na Yanar Gizon da aka yi amfani da shi a sama misali ana iya saukewa daga WEBPSampleImage.webp da fitarwa daga Converted.jpg.
WebP zuwa PNG a Java
Wannan sashe yana tattauna cikakkun bayanai kan yadda ake canza WebP zuwa tsarin PNG ba tare da amfani da ma’ajiyar girgije ba.
- Da farko, ƙirƙiri wani abu na ImagingApi ta amfani da keɓaɓɓen takaddun shaida
- Na biyu, loda hoton WebP ta amfani da hanyar readAllBytes(…) sannan ka wuce zuwa tsararrun byte[]
- Na uku, ƙirƙirar wani abu na CreateConvertedImageRequest wanda ke ɗaukar sunan hoton WebP da tsarin pNG azaman mahawara.
- Yanzu kira hanyar ƙirƙirarConvertedImage(…) don canza WebP zuwa PNG. Ana mayar da fitarwa azaman rafin amsawa
- A ƙarshe, ajiye sakamakon PNG zuwa faifan gida ta amfani da abu FileOutputStream
// Samu ClientID da ClientSecret daga https://dashboard.aspose.cloud/
String clientId = "bb959721-5780-4be6-be35-ff5c3a6aa4a2";
String clientSecret = "4d84d5f6584160cbd91dba1fe145db14";
// haifar da Hoto abu
ImagingApi imageApi = new ImagingApi(clientSecret, clientId);
// ɗora hoton WebP daga faifan gida
File file1 = new File("WEBPSampleImage.webp");
byte[] imageStream = Files.readAllBytes(file1.toPath());
// saka tsarin fitarwa azaman PNG
String format = "png";
// Ƙirƙiri abin buƙatar canza hoto
CreateConvertedImageRequest convertRequest = new CreateConvertedImageRequest(imageStream,format,null,null);
// canza WebP zuwa PNG kuma dawo da fitarwa a cikin rafin amsawa
byte[] resultantImage = imageApi.createConvertedImage(convertRequest);
// Ajiye sakamakon PNG zuwa faifan gida
FileOutputStream fos = new FileOutputStream("/Users/nayyer/Documents/" + "Converted.png");
fos.write(resultantImage);
fos.close();
Lura: - Idan muna buƙatar adana sakamakon PNG akan ajiyar girgije, wuce cikakkun bayanan hanyar PNG yayin ƙirƙirar wani abu na CreateConvertedImageRequest. A wannan hanya, mataki na ƙarshe don adana rafin fitarwa zuwa tuƙi na gida za a tsallake shi.
Yanar gizo zuwa GIF ta amfani da Umarnin CURL
Hakanan za’a iya samun dama ga REST API ta umarnin cURL, don haka a cikin wannan sashe, zamuyi canjin WebP zuwa GIF ta amfani da umarnin CURL. Yanzu abin da ake bukata shine don samar da alamar samun damar JWT (dangane da shaidar abokin ciniki) ta amfani da umarni mai zuwa.
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Bayan JWT da aka generated, da fatan za a aiwatar da wadannan umurnin yi WebP zuwa GIF hira.
curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/WEBPSampleImage.webp/convert?format=GIF" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-o "Converted,gif"
Kammalawa
Wannan labarin ya bayyana duk cikakkun bayanai game da yadda ake canza WebP zuwa JPEG, WebP zuwa PNG da yadda ake yin WebP zuwa GIF ta amfani da REST API. Mun koyi matakai don canza WebP zuwa JPG da tsari ko canza webp zuwa gif ta amfani da umarnin cURL. Muna ƙarfafa ku sosai don bincika Takardun Samfura wanda shine tushen bayanai mai ban mamaki don koyo game da wasu abubuwan ban sha’awa waɗanda API ɗin ke bayarwa.
Hakanan kuna iya yin la’akari da gwada fasalin API ta hanyar SwaggerUI a cikin mai binciken gidan yanar gizo kuma idan kuna buƙatar zazzagewa da gyara lambar tushe na Cloud SDK, yana kan GitHub (an buga shi ƙarƙashin lasisin MIT) . A ƙarshe, idan kun gano kowace matsala yayin amfani da API, kuna iya yin la’akari da kusantar mu don ƙuduri mai sauri ta hanyar dandalin tallafi na kyauta 9.
Labarai masu alaka
Da fatan za a ziyarci hanyoyin haɗin yanar gizon don ƙarin koyo game da: